我对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'
当我使用导入数据集时,lmplot正常工作:
df.head()
Asset Paid Organic
0 A 23 2
1 B 231 234
2 C 321 242
我做错了什么? sns.regplot没有给我这个错误,但是我想使用hue来更容易理解更大的数据集。我尝试了很多东西来解决这个问题并且无法找到任何解决方案。 xlxs和csv文件都给我这个错误。
答案 0 :(得分:3)
sns.lmplot(x=df["TestX"],y=df["TestY"], data=df)
x,y:字符串,可选 输入变量,这些应该是数据中的列名。
您可以再次尝试:
sns.lmplot(x="TestX",y="TestY", data=df)
答案 1 :(得分:0)
import pandas as pd
import numpy as np
import matplotlib
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="darkgrid")
df = pd.read_csv('data.csv')
sns.lmplot(x="Duration", y="Maxpulse", data=df)
plt.show()
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()
“Duration”和“Maxpluse”是列名。
这是我的代码的输出: