Skip to main content
 首页 » 编程设计

python之seaborn 的 lmplot : KeyError [ . ..] 不在索引中; excel 和 csv 导入

2025年05月04日21dflying

我是 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 

当我使用 import Datasets 时,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 没有给我那个错误,但我想使用色调,使更大的数据集更容易理解。我尝试了很多方法来解决这个问题,但找不到任何解决方案。 xlxs 和 csv 文件都给我这个错误。

请您参考如下方法:

不要使用

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

来自 the documentation :

x, y : strings, optional

Input variables, these should be column names in data.

你可以再试一次:

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