gpt4 book ai didi

python - 关于使用 dict 分割数据帧的错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:15:27 28 4
gpt4 key购买 nike

训练数据如下所示:

p,x,s,n,t,p,f,c,n,k,e,e,s,s,w,w,p,w,o,p,k,s,u
e,x,s,y,t,a,f,c,b,k,e,c,s,s,w,w,p,w,o,p,n,n,g
e,b,s,w,t,l,f,c,b,n,e,c,s,s,w,w,p,w,o,p,n,n,m
p,x,y,w,t,p,f,c,n,n,e,e,s,s,w,w,p,w,o,p,k,s,u
e,x,s,g,f,n,f,w,b,k,t,e,s,s,w,w,p,w,o,e,n,a,g
e,x,y,y,t,a,f,c,b,n,e,c,s,s,w,w,p,w,o,p,k,n,g
e,b,s,w,t,a,f,c,b,g,e,c,s,s,w,w,p,w,o,p,k,n,m

第一栏是关于该蘑菇是否可以食用的标签。(e:可食用,p:有毒)我想根据是否可食用将这些数据分成两部分。我的代码如下:

mushdf = pd.read_csv('agaricus-lepiota.data') #load in two data for mushroom and iris
mushdf.columns = ['edible?','cap-shape','cap-surface','cap-color','bruises?','odor',
'gill-attachment','gill-spacing','gill-size','gill-color',
'stalk-shape','stalk-root','stalk-surface-above-ring','stalk-surface-below-ring',
'stalk-color-above-ring','stalk-color-below-ring','veil-type','veil-color',
'ring-number','ring-type','spore-print-color','population','habitat']
print(mushdf)
mushdic = {key: mushdf for (key, mushdf) in mushdf.groupby('edible?')}
for key in mushdic:
print(f'mushdic[{key}]')
print(mushdic[key])
print('-'*50)

问题是,当我删除第 2 行到第 6 行中的 mushdf.columns 时,此代码有效。但是,当我执行 mushdf.columns 时,终端返回错误消息。

用另一列同样的方法就可以了。例如,mushdic = {key: mushdf for (key, mushdf) in mushdf.groupby('bruises?')} 运行正确。

我对此一无所知。

Traceback (most recent call last):
File "e:\Visual Studio Project\LiMing\vs2017_python\.vscode\helloworld.py", line 11, in <module>
mushdic = {key: mushdf for (key, mushdf) in mushdf.groupby('edible?')}
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pandas\core\generic.py", line 7894, in groupby
**kwargs
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pandas\core\groupby\groupby.py", line 2522, in groupby
return klass(obj, by, **kwds)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pandas\core\groupby\groupby.py", line 391, in __init__
mutated=self.mutated,
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pandas\core\groupby\grouper.py", line 621, in _get_grouper
raise KeyError(gpr)
KeyError: 'edible?'
The terminal process terminated with exit code: 1

最佳答案

pandas.read_csv意味着 csv 文件中的第一行是标题。由于您的 csv 文件没有标题,因此您需要在导入过程中告知这一点。您还应该在此处传递列名称:

mushdf = pd.read_csv('agaricus-lepiota.data', header=None, names=[
'edible?','cap-shape','cap-surface','cap-color','bruises?','odor',
'gill-attachment','gill-spacing','gill-size','gill-color',
'stalk-shape','stalk-root','stalk-surface-above-ring','stalk-surface-below-ring',
'stalk-color-above-ring','stalk-color-below-ring','veil-type','veil-color',
'ring-number','ring-type','spore-print-color','population','habitat'])

关于python - 关于使用 dict 分割数据帧的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58476374/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com