1

I have a dataframe look like this:-

df1.set_index('date', inplace=True)

date        symbol   roe
2019-12-31  FMC 21.2189
2018-12-31  FMC 16.7456
2017-12-31  FMC -4.9167
2016-12-31  FMC 6.5580
2015-12-31  FMC -11.1408
2014-12-31  FMC 19.6675
2013-12-31  FMC 23.6372
2012-12-31  FMC 29.7916
2011-12-31  FMC 32.2291
2010-12-31  FMC 18.3737
2009-12-31  FMC 22.6811
2008-12-31  FMC 35.8547
2007-12-31  FMC 15.6253
2006-12-31  FMC 14.2645
2005-12-31  FMC 11.2999
2019-12-31  VMC 21.2189
2018-12-31  VMC 16.7456
2017-12-31  VMC -4.9167
2016-12-31  VMC 6.5580
2015-12-31  VMC -11.1408
2014-12-31  VMC 19.6675
2013-12-31  VMC 23.6372
2012-12-31  VMC 29.7916
2011-12-31  VMC 32.2291
2010-12-31  VMC 18.3737
2009-12-31  VMC 22.6811
2008-12-31  VMC 35.8547
2007-12-31  VMC 15.6253
2006-12-31  VMC 14.2645
2005-12-31  VMC 11.2999
2019-12-31  APD 21.2189
2018-12-31  APD 16.7456
2017-12-31  APD -4.9167
2016-12-31  APD 6.5580
2015-12-31  APD -11.1408
2014-12-31  APD 19.6675
2013-12-31  APD 23.6372
2012-12-31  APD 29.7916
2011-12-31  APD 32.2291
2010-12-31  APD 18.3737
2009-12-31  APD 22.6811
2008-12-31  APD 35.8547
2007-12-31  APD 15.6253
2006-12-31  APD 14.2645
2005-12-31  APD 11.2999
2019-12-31  MLM 21.2189
2018-12-31  MLM 16.7456
2017-12-31  MLM -4.9167
2016-12-31  MLM 6.5580
2015-12-31  MLM -11.1408
2014-12-31  MLM 19.6675
2013-12-31  MLM 23.6372
2012-12-31  MLM 29.7916
2011-12-31  MLM 32.2291
2010-12-31  MLM 18.3737
2009-12-31  MLM 22.6811
2008-12-31  MLM 35.8547
2007-12-31  MLM 15.6253
2006-12-31  MLM 14.2645
2005-12-31  MLM 11.2999
2019-12-31  NEM 21.2189
2018-12-31  NEM 16.7456
2017-12-31  NEM -4.9167
2016-12-31  NEM 6.5580
2015-12-31  NEM -11.1408
2014-12-31  NEM 19.6675
2013-12-31  NEM 23.6372
2012-12-31  NEM 29.7916
2011-12-31  NEM 32.2291
2010-12-31  NEM 18.3737
2009-12-31  NEM 22.6811
2008-12-31  NEM 35.8547
2007-12-31  NEM 15.6253
2006-12-31  NEM 14.2645
2005-12-31  NEM 11.2999

I have already set the date as index and I use groupby function to plot a graph, and the code look like this:-

df1.groupby('symbol')['roe'].plot(figsize = (8, 6), legend=True)

The output display 1 line only. I except the output will display 5 different drawings because in the dataframe there has 5 different symbol, may I know how can I solve the problem?

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
janicewww
  • 323
  • 1
  • 10
  • You need to reshape your dataframe with symbols as columns or use seaborn with hue='symbol'. – Scott Boston Oct 20 '20 at 18:50
  • I followed the post: https://stackoverflow.com/questions/41494942/pandas-dataframe-groupby-plot/56652699 and he has the same issue like this, may I know why he can use groupby function to draw the plot? – janicewww Oct 20 '20 at 19:45

1 Answers1

2

You may need to review generation/source of your data. Your plotting code is totally valid, yielding:

enter image description here

which seems to be five identical plots. A quick pivot shows that you indeed have identical roe for all symbols:

df.pivot('date','symbol','roe')

Output:

symbol          APD      FMC      MLM      NEM      VMC
date                                                   
2005-12-31  11.2999  11.2999  11.2999  11.2999  11.2999
2006-12-31  14.2645  14.2645  14.2645  14.2645  14.2645
2007-12-31  15.6253  15.6253  15.6253  15.6253  15.6253
2008-12-31  35.8547  35.8547  35.8547  35.8547  35.8547
2009-12-31  22.6811  22.6811  22.6811  22.6811  22.6811
2010-12-31  18.3737  18.3737  18.3737  18.3737  18.3737
2011-12-31  32.2291  32.2291  32.2291  32.2291  32.2291
2012-12-31  29.7916  29.7916  29.7916  29.7916  29.7916
2013-12-31  23.6372  23.6372  23.6372  23.6372  23.6372
2014-12-31  19.6675  19.6675  19.6675  19.6675  19.6675
2015-12-31 -11.1408 -11.1408 -11.1408 -11.1408 -11.1408
2016-12-31   6.5580   6.5580   6.5580   6.5580   6.5580
2017-12-31  -4.9167  -4.9167  -4.9167  -4.9167  -4.9167
2018-12-31  16.7456  16.7456  16.7456  16.7456  16.7456
2019-12-31  21.2189  21.2189  21.2189  21.2189  21.2189
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74