pandas 如何指定 x 和 y 轴以在 Python 中绘制数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45738773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify x and y axis for plotting dataframe in Python
提问by ejshin1
I would like to spcify x and y axis to draw data in dataframe in Python.
我想指定 x 和 y 轴以在 Python 中的数据框中绘制数据。
For example, I have four columns in dataframe. And here's my code.
例如,我在数据框中有四列。这是我的代码。
df.plot(x=df['a'], y=df['b'], label=df['c'])
It throws an error saying: These values come from 'b' column.
它抛出一个错误说:这些值来自“b”列。
"KeyError: '[ 500.8 567.2 487.2 444.4 1371.6 714.4 1157.4
476.8 345.4\n 1076.4 881.8 813. 452.6 663.6 606.8 469.2 805.2 487.4\n 497.8 440. 127. 68.6 1494.2 716.4 1447. 97.8 110.\n 1126.4 1422.8 92.4 1000.8] not in index'"
“KeyError异常:“[500.8 567.2 487.2 444.4 1371.6 714.4 1157.4
476.8 345.4 \ n 1076.4 881.8 813 452.6 663.6 606.8 469.2 805.2 487.4 \ n 497.8 440 127 68.6 1494.2 716.4 97.8 1447 110 \ n 1126.4 1422.8 92.4 1000.8]在不指数'”
Thank you for the help in advance.
提前感谢您的帮助。
回答by Trion
df.plottakes the column labels as x and y, not the data itself
df.plot将列标签作为 x 和 y,而不是数据本身
df.plot(x='a', y='b')
might work
df.plot(x='a', y='b')
可能有用
Use https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.htmlas reference to the arguments needed in pandas
使用https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html作为 Pandas 中所需参数的参考
https://pandas.pydata.org/pandas-docs/stable/visualization.htmlas examples
https://pandas.pydata.org/pandas-docs/stable/visualization.html作为示例
回答by Bharath
Its a simple mistake. Instead of df['a']
... just past the column name i.e
这是一个简单的错误。而不是df['a']
...刚刚过去的列名即
df.plot(x='a', y='b', label='c')
回答by IanS
The correct use is to pass column names:
正确的用法是传递列名:
df.plot(x='a', y='b', label='c')
The error is pandas trying to use the entire column df['b']
as a column name.
错误是Pandas试图使用整个列df['b']
作为列名。