Barnaby Cooper
Barnaby Cooper

Reputation: 43

Error when looping through DF to plot each column

I have a df as below. I am trying to loop over the df and plot on 5 separate plots a timeseries of each column using the following code.

for col_name in loop_returns.columns.values[0:10]:
  loop_returns.plot(y=loop_returns[col_name])
Ticker       EZJ    INRG      ISF   JDW     KO
Date                    
2016-02-24  15.19   3.4     5.922   7.05    0.43564
2016-02-25  14.77   3.474   5.926   7.215   0.4396
2016-02-26  15.06   3.563   6.041   7.245   0.4404
2016-02-29  14.77   3.484   6.023   7.25    0.4317
2016-03-01  15.1    3.541   6.065   7.195   0.4338
2016-03-02  15.52   3.583   6.154   7.215   0.4347
2016-03-03  15.14   3.585   6.126   7.255   0.4361
2016-03-04  14.92   3.64    6.144   7.27    0.4378

But this is throwing the following error:

KeyError: "None of [Float64Index([            15.19,             14.77,             15.06,\n                          14.77,              15.1,             15.52,\n                          15.14,             14.92,             15.46,\n                          15.24,\n              ...\n                            8.2,             8.282,             7.982,\n                          7.676,              7.55,             7.816,\n                            8.2,               8.2,             8.054,\n              8.017999999999999],\n             dtype='float64', name='Ticker', length=1283)] are in the [columns]"

Upvotes: 0

Views: 39

Answers (1)

pakpe
pakpe

Reputation: 5479

If you are trying to produce and save separate plots without subplotting, you can loop through like this:

for col in df.columns[1:]:
    plt.plot(df[df.columns[0]],df[col])
    plt.savefig(col)
    plt.show()

Upvotes: 2

Related Questions