python - Plotting and GroupBy methods -
i learning take advantage of dataframes in pandas , use groupby methods produce plots of following:
i have 2 dataframes, 1 x-axis information , 1 y. in each dataframe there 3 versions of data, 'a', 'b', 'c'. need plot of y vs x each of (i.e. 3 lines).
example code:
df_x <class 'pandas.core.frame.dataframe'> int64index: 100 entries, 0 99 data columns (total 3 columns): 100 non-null values b 100 non-null values c 100 non-null values dtypes: float64(2), object(1) df_y <class 'pandas.core.frame.dataframe'> int64index: 100 entries, 0 99 data columns (total 3 columns): 100 non-null values b 100 non-null values c 100 non-null values dtypes: float64(2), object(1)
is there quick way produce desired plot avoiding loops , using pandas methods? i'm thinking of merging both frames , using groupby methods, don't know how go doing exactly.
thanks!
i think can plot directly using pyplot:
in [11]: plot(df_x, df_y) # matplotlib.pyplot.plot out[11]: [<matplotlib.lines.line2d @ 0x109c02910>, <matplotlib.lines.line2d @ 0x109c02b90>, <matplotlib.lines.line2d @ 0x109c02ed0>]
it seems need set legend after though:
pylab.legend(df_x.columns)
if wanted reshape data form use .plot
, perhaps use:
in [21]: df_x = pd.dataframe([[1,2,1],[2,3,4]], columns=list('abc')) in [22]: df_y = pd.dataframe([[2,6,1],[4,9,4]], columns=list('abc')) in [23]: pd.dataframe({'x': df_x.stack(), 'y': df_y.stack()}).reset_index(level=1).pivot('x', 'level_1', 'y') out[23]: level_1 b c x 1 2 nan 1 2 4 6 nan 3 nan 9 nan 4 nan nan 4
this going considerably less efficient (take lot more space needs to), since contains lot of missing data.
Comments
Post a Comment