熊猫图:带索引的散点图

Man*_*rma 5 python plot matplotlib pandas

我正在尝试从 Pandas 数据帧创建散点图,但我不想使用 matplotlib plt。以下是脚本

df:
group people value
   1    5    100
   2    2    90
   1    10   80
   2    20   40
   1    7    10
Run Code Online (Sandbox Code Playgroud)

我想在 x 轴上创建一个带有索引的散点图,只使用熊猫数据框

df.plot.scatter(x = df.index, y = df.value)
Run Code Online (Sandbox Code Playgroud)

它给了我一个错误

Int64Index([0, 1, 2, 3, 4], dtype='int64') not in index
Run Code Online (Sandbox Code Playgroud)

我不想使用

plt.scatter(x = df.index, y = df.value)
Run Code Online (Sandbox Code Playgroud)

如何使用熊猫数据框执行此图

ank*_*_91 9

您可以尝试使用:

df.reset_index().plot.scatter(x = 'index', y = 'value')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 这确实有效,因为“reset_index()”函数从旧的未命名索引创建一个新列,并默认将其称为“index”,然后由“plot.scatter()”识别 (2认同)