gpt4 book ai didi

python - 我的代码在读取此 csv 文件并将第一列命名为 "team"时遇到问题

转载 作者:行者123 更新时间:2023-12-01 00:44:23 24 4
gpt4 key购买 nike

由于某种原因,我无法将 csv 文件正确读入我的代码中。

Here is my csv file

这是我的代码:

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

对于这个用例,我认为这个选择不会对您产生任何影响。

df_playoffs = pd.read_csv('/Users/hannahbeegle/Desktop/playoff_teams.csv', encoding='latin-1', index_col = 'Team', sort = False)
df_playoffs.fillna('None', inplace=True)

错误

错误的最后一行是:

ValueError: Index team invalid

查看您的 CSV,您没有 team 列。您不能在不存在的列上声明索引。您需要创建一个包含团队名称的新列,或使用已存在的列。

关于python - 我的代码在读取此 csv 文件并将第一列命名为 "team"时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57097400/

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