我是python的新手,所以请多多包涵。我无法使用seaborn的lmplot可视化excel / csv文件。这段代码:

%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df=pd.read_csv("C:/Users/me/Documents/Jupyter Notebooks/Seaborn/Test.csv")
sns.set_style('whitegrid')
sns.lmplot(x=df["TestX"],y=df["TestY"], data=df)


给我这个错误:

    ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-41-00ad8b882663> in <module>()
----> 1 sns.lmplot(x=df["TestX"],y=df["TestY"], data=df)

~\Anaconda3\lib\site-packages\seaborn\regression.py in lmplot(x, y, data, hue, col, row, palette, col_wrap, size, aspect, markers, sharex, sharey, hue_order, col_order, row_order, legend, legend_out, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, x_jitter, y_jitter, scatter_kws, line_kws)
    550     need_cols = [x, y, hue, col, row, units, x_partial, y_partial]
    551     cols = np.unique([a for a in need_cols if a is not None]).tolist()
--> 552     data = data[cols]
    553
    554     # Initialize the grid

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2131         if isinstance(key, (Series, np.ndarray, Index, list)):
   2132             # either boolean or fancy integer index
-> 2133             return self._getitem_array(key)
   2134         elif isinstance(key, DataFrame):
   2135             return self._getitem_frame(key)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_array(self, key)
   2175             return self._take(indexer, axis=0, convert=False)
   2176         else:
-> 2177             indexer = self.loc._convert_to_indexer(key, axis=1)
   2178             return self._take(indexer, axis=1, convert=True)
   2179

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
   1267                 if mask.any():
   1268                     raise KeyError('{mask} not in index'
-> 1269                                    .format(mask=objarr[mask]))
   1270
   1271                 return _values_from_object(indexer)

KeyError: '[  2  23 231 234 242 321] not in index'


我发现索引有问题,但是对我来说(如我所说,我是新手),索引似乎设置正确:

df.head()

    Asset   Paid    Organic
0   A   23  2
1   B   231     234
2   C   321     242


当我使用导入数据集时,lmplot可以正常工作:

%matplotlib inline
import seaborn as sns
sns.set(color_codes=True)
tips = sns.load_dataset("tips")
g = sns.lmplot(x="total_bill", y="tip", data=tips, hue="smoker")


我究竟做错了什么? sns.regplot没有给我这个错误,但是我想使用hue来使更大的数据集更容易理解。我尝试了很多方法来解决此问题,但找不到任何解决方案。 xlxs和csv文件都给我这个错误。

最佳答案

sns.lmplot(x=df["TestX"],y=df["TestY"], data=df)


x,y:字符串,可选
输入变量,这些变量应该是数据中的列名。

您可以使用以下方法重试:

sns.lmplot(x="TestX",y="TestY", data=df)

10-01 11:10