pandas ValueError:在将索引与seaborn lineplot一起使用时无法解释输入“索引”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52256929/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:01:00  来源:igfitidea点击:

ValueError: Could not interpret input 'index' when using index with seaborn lineplot

pythonpython-2.7pandasseaborn

提问by MaxS

I want the use the index of a pandas DataFrame as x value for seaborn and is raised a value error. A small test example:

我希望使用Pandas DataFrame 的索引作为 seaborn 的 x 值并引发值错误。一个小测试示例:

import pandas as pd
import seaborn as sns
sns.lineplot(x='index',y='test',hue='test2',data=pd.DataFrame({'test':range(9),'test2':range(9)}))

It raises:

它提出:

ValueError: Could not interpret input 'index'

is it not possible to use the index as x values? What am I doing wrong? Python 2.7, seaborn 0.9

不能将索引用作 x 值吗?我究竟做错了什么?Python 2.7,seaborn 0.9

回答by Sheldore

I would rather prefer to use it this way. You need to remove hueas I assume it has a different purpose which doesn't apply in your current DataFrame because you have a single line. Visit the official docs herefor more info.

我宁愿这样使用它。您需要删除,hue因为我认为它有不同的用途,不适用于您当前的 DataFrame,因为您只有一行。请在此处访问官方文档以获取更多信息。

df=pd.DataFrame({'test':range(9),'test2':range(9)})
sns.lineplot(x=df.index, y='test', data=df)

Output

输出

enter image description here

enter image description here

回答by ImportanceOfBeingErnest

You would need to make sure the string you provide to the xargument is actually a columnin your dataframe. The easiest solution to achieve that is to reset the index of the dataframe to convert the index to a column.

您需要确保提供给x参数的字符串实际上是数据框中的一。最简单的解决方案是重置数据帧的索引以将索引转换为列。

sns.lineplot(x='index', y='test', data=pd.DataFrame({'test':range(9),'test2':range(9)}).reset_index())