python - 在子集 Pandas 数据帧时忽略 KeyError

标签 python pandas error-handling

我有一个 pandas dataframe df 列 city1, city2, city3, city4, 城市 5。我有一个列表 my_cities = ["city1","city3","city10"]。我想根据 my_cities 中的列对 df 进行子集化。当我这样做时,

my_cities = ["city1","city3","city10"]

df_my_cities = df[my_cities]

我收到错误 KeyError: "['city10'] not in index"

如果 my_cities 中的元素不在 df 中,我如何告诉代码继续进行?

最佳答案

您可以使用 intersection在所有列和 list 之间:

df_my_cities = df[df.columns.intersection(my_cities)]

示例:

df = pd.DataFrame({'city1':['s', 'e'],
                   'city2':['e','f'],
                   'city3':['f','g'],
                   'city4':['r','g'],
                   'city5':['t','m']})

print (df)
  city1 city2 city3 city4 city5
0     s     e     f     r     t
1     e     f     g     g     m

my_cities = ["city1","city3","city10"]
df_my_cities = df[df.columns.intersection(my_cities)]
print (df_my_cities)
  city1 city3
0     s     f
1     e     g

或者 numpy.intersect1d :

df_my_cities = df[np.intersect1d(df.columns, my_cities)]
print (df_my_cities)
  city1 city3
0     s     f
1     e     g

关于python - 在子集 Pandas 数据帧时忽略 KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43618406/

相关文章:

python - 根据值列 pandas 计算名称

sorting - Pandas 多索引排序

python - 在 Python 中捕获嵌套异常

php - jquery + PHP : catching php error from ajax

Python try/except 问题与 SMTPLib

python - 用 mechanicalsoup 下载文件

python - 如何记录和测试 Python 2 中形式参数所需的接口(interface)?

Python - ConnectionRefusedError、urllib3.exceptions.NewConnectionError 和 Colorama

python - 查找行并删除它 - Pandas DataFrame

python Pandas : returning more then one field value when applying function to a data frame row