由于某些原因,我无法将csv文件正确地读入代码。你知道吗
这是我的密码:
df_playoffs = pd.read_csv('/Users/hannahbeegle/Desktop/playoff_teams.csv', encoding='latin-1', index_col = 'team')
df_playoffs.fillna('None', inplace=True)
以下是错误消息:
Warning (from warnings module):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py", line 6692
sort=sort)
FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.
To accept the future behavior, pass 'sort=False'.
To retain the current behavior and silence the warning, pass 'sort=True'.
Traceback (most recent call last):
File "/Users/hannahbeegle/Desktop/Baseball.py", line 131, in <module>
index_col = 'team')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py", line 702, in parser_f
return _read(filepath_or_buffer, kwds)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py", line 435, in _read
data = parser.read(nrows)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py", line 1139, in read
ret = self._engine.read(nrows)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py", line 2069, in read
index, names = self._make_index(data, alldata, names)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py", line 1541, in _make_index
index = self._get_simple_index(alldata, columns)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py", line 1574, in _get_simple_index
i = ix(idx)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/parsers.py", line 1569, in ix
raise ValueError('Index %s invalid' % col)
ValueError: Index team invalid
我在这里看到两个问题:
警告
您的警告是由于Python库在将来的版本中将改变其行为的方式造成的。这是一种尝试,使您今天编写的代码在将来工作时不会出现意外的输出更改。你知道吗
过去,参数
sort
的默认值为True
。这就是为什么警告告诉您要设置sort=True
以保持库的当前默认行为。如果不希望对数据帧进行排序(因为它将是将来的默认值,请执行相反的操作:sort=False
。你知道吗对于这个用例,我认为这个选择不会对您产生任何影响。你知道吗
错误
错误的最后一行是:
查看CSV,您没有
team
列。不能对不存在的列声明索引。您需要创建一个包含团队名称的新列,或者使用已经存在的列。你知道吗我能够重现您的问题,并将我的CSV列名从'team'更改为'team'以更正问题。开发人员端的简单拼写错误。错误显示panda试图读取不存在的列标题。你知道吗
您可以在以下位置更改索引列字符串参数:
至
测试并使用此代码:
季后赛_团队.csv样品
相关问题 更多 >
编程相关推荐