我从csv文件中读取数据,我只需要特定列,我需要选择一些与我将传递的前缀匹配的行,我正在执行以下操作:
account_name = 'Acc1'
df_ec2tpm=pd.read_csv(ec2File, usecols = ["Internal IP", "Instance Id", "PatchGroup","Account"], index_col=3)
df_ec2tpm.loc[df_ec2tpm['Account'].str.startswith(account_name)]
print (df_ec2tpm)
如果我打印结果,我会看到以下内容:
Account,Instance Id,PatchGroup,Internal IP
Acc1-dev,i-0aaa9525f4999999,Windows,192.168.3.20
Acc1-dev,i-0aaa9525f5000000,Windows,192.168.3.21
Acc2-prod,i-0aaa9525f5000001,Windows,192.168.3.22
Acc1-prod,i-0aaa9525f5000002,Windows,192.168.3.23
Acc1-prod,i-0aaa9525f5000003,Windows,192.168.3.24
Acc2-dev,i-0aaa9525f5000004,Windows,192.168.3.25
Acc2-dev,i-0aaa9525f5000005,Windows,192.168.3.26
Acc2-dev,i-0aaa9525f5000006,Windows,192.168.3.27
但当我尝试使用df_ec2tpm.loc进行选择时,它失败了,出现了错误
File "C:\Users\marr\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 2899, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Account'
怎么了
理想情况下,我会这样做
df_ec2=df_ec2tpm.loc[df_ec2tpm['Account'].str.startswith(account_name)]
因为我需要将这个数据集与另一个数据集合并
问题是“Account”现在是数据帧的索引,而不是它的列。这就是为什么你会得到KeyError。只需删除
index_col=3
而且,
df_ec2tpm = df_ec2tpm.loc[df_ec2tpm['Account'].str.startswith(account_name), :]
切片输出一个新的数据帧,而不修改数据帧相关问题 更多 >
编程相关推荐