python - key 错误 : "None of [Float64Index([34.62365962451697, 30.28671076822607, 35.84740876993872], dtype=' float6 4')] are in the [columns]"

标签 python pandas dataframe plot

这是我的数据框:

          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: "None of [Float64Index([34.62366, 30.286710999999997, 35.847409000000006], dtype='float64')] are in the [columns]"

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

最佳答案

如果想要使用DataFrame.plot仅传递列名称:

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

如果想要使用matplotlib.pyplot.plot那么你可以通过 Series:

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

关于python - key 错误 : "None of [Float64Index([34.62365962451697, 30.28671076822607, 35.84740876993872], dtype=' float6 4')] are in the [columns]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59694029/

相关文章:

python - pandas read_sql 在列名中删除点

python - 如何聚合行的子集并附加到 MultiIndexed Pandas DataFrame?

python - Pandas系列应用lambda : NoneType found,,但系列中只有str和list

python - 使用 OneToOneField 扩展 Django Admin UserCreationForm

python - 汇总列以形成新的数据框

python - 如何在多级索引中为某些列(但不是全部)交换级别

python - python中的数据帧值比较

Python - 使用 pandas 格式化 excel 单元格

python - 如何使用pygame和openGL修改相机的 View

python - jupyter 上没有模块 cv