Reputation: 614
I have a dataset that looks like this.
Date Name High Value
2017-12-31 Bitcoin 14377.40 18723.76
2017-12-30 Bitcoin 14681.90 18766.88
2017-12-29 Bitcoin 15279.00 18755.70
2017-12-28 Bitcoin 15888.40 18820.54
... ... ... ...
2017-01-08 CannaCoin 0.01 0.02
2017-01-07 CannaCoin 0.01 0.02
2017-01-06 CannaCoin 0.01 0.02
2017-01-05 CannaCoin 0.02 0.01
Date is the index column and is in the datetime format. My dataset is big and has more than one item in the Name
column. The dates range from the beginning of the year till the end. Also, not all items are of the same length. Most should finish at the end of the year but they don't necessarily start in the beginning of it, they can start later.
What I would like to do is, group by Name
values, and for each create a separate line on the same graph/plot. Value
should be on the y axis.
Because I am used to R, what I did was:
df.groupby("Name")["Value"].plot()
What I got is a warning:
UserWarning: Attempting to set identical left==right results in singular transformations; automatically expanding. left=17531.0, right=17531.0 'left=%s, right=%s') % (left, right))
Also, the plot looked like this:
As observable, half of the values are missing, as they are outside the plotting area, dates are in a descending instead of an ascending order and half of the plot is empty.
How can I fix this, so that the entire plot will be visible, with the dates in a correct order?
Upvotes: 0
Views: 1023
Reputation: 1919
you can pivot on the "Name" and then pass the dataframe to the plot.
pd.pivot_table(data=df, values="Value",columns="Name",index="Date").plot()
Example:
In[]:
idx = ["2017-12-28","2017-12-29","2017-12-30","2017-12-31"] * 2
name = ['Bitcoin'] * 4 + ['CannaCoin'] * 4
vals = np.random.rand(8) * 1000
df = pd.DataFrame({"Date":idx, "Name":name, "Value":vals})
print(df)
Out[]:
Date Name Value
0 2017-12-28 Bitcoin 788.547631
1 2017-12-29 Bitcoin 572.695484
2 2017-12-30 Bitcoin 661.859195
3 2017-12-31 Bitcoin 205.473883
4 2017-12-28 CannaCoin 270.291858
5 2017-12-29 CannaCoin 683.827404
6 2017-12-30 CannaCoin 447.808772
7 2017-12-31 CannaCoin 616.927833
In[]:
pd.pivot_table(data=df, values="Value",columns="Name",index="Date").plot()
Upvotes: 1