【问题标题】:KeyError: "None of [Float64Index([34.62365962451697, 30.28671076822607, 35.84740876993872], dtype='float64')] are in the [columns]"KeyError:“[Float64Index([34.62365962451697, 30.28671076822607, 35.84740876993872], dtype='float64')] 中没有 [列]”
【发布时间】:2020-04-28 19:59:16
【问题描述】:

这是我的数据框:

          x1         x2  y  x2_modified
0  34.623660  78.024693  0    99.294362
1  30.286711  43.894998  0   110.085855
2  35.847409  72.902198  0    96.249345

当我尝试访问x1 列时,它完美地完成了:

>>> print(df.x1)
0    34.623660
1    30.286711
2    35.847409
Name: x1, dtype: float64

但是当我尝试绘制它时,

df.plot(x = df.x1, y = df.x2_modified)

它给出了上述错误:

KeyError:“[Float64Index([34.62366, 30.286710999999997, 35.847409000000006], dtype='float64')] 中没有 [列]”

我不明白原因,请帮助。谢谢。

【问题讨论】:

    标签: python pandas dataframe plot


    【解决方案1】:

    如果想使用DataFrame.plot 只传递列名:

    df.plot(x = 'x1', y = 'x2_modified')
    

    如果要使用matplotlib.pyplot.plot,则可以通过Series

    plt.plot(df.x1, df.x2_modified)
    

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2021-01-17
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多