我的代码无法读取此csv文件并将第一列命名为“team”

2024-04-18 22:41:15 发布

您现在位置:Python中文网/ 问答频道 /正文

由于某些原因,我无法将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

Tags: inpyiopandasreadindexlibpackages
2条回答

我在这里看到两个问题:

警告

您的警告是由于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列。不能对不存在的列声明索引。您需要创建一个包含团队名称的新列,或者使用已经存在的列。你知道吗

我能够重现您的问题,并将我的CSV列名从'team'更改为'team'以更正问题。开发人员端的简单拼写错误。错误显示panda试图读取不存在的列标题。你知道吗

您可以在以下位置更改索引列字符串参数:

df_playoffs = pd.read_csv('/Users/hannahbeegle/Desktop/playoff_teams.csv', encoding='latin-1', index_col = 'team')

df_playoffs = pd.read_csv('/Users/hannahbeegle/Desktop/playoff_teams.csv', encoding='latin-1', index_col = 'Team')

测试并使用此代码:

import pandas as pd 

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

print(data.head())

季后赛_团队.csv样品

Team                                                                         
Dummy data0       None       None       None       None       None       None
Dummy data1       None       None       None       None       None       None
Dummy data2       None       None       None       None       None       None
Dummy data3       None       None       None       None       None       None
Dummy data4       None       None       None       None       None       None

相关问题 更多 >