pandas数据框从数据集中选择与开头匹配的行

问题描述 投票:0回答:1

我从一个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)]

因为我需要将此数据集与另一个数据集合并。

python pandas dataframe select rows
1个回答
0
投票

问题在于,“帐户”现在是数据框的索引,而不是其列。这就是为什么您得到KeyError的原因。只需删除index_col=3

另外,df_ec2tpm = df_ec2tpm.loc[df_ec2tpm['Account'].str.startswith(account_name), :]切片将输出新的数据帧,而无需就地修改数据帧。

© www.soinside.com 2019 - 2024. All rights reserved.