问题描述
我有一个熊猫数据框df,其列为city1,city2,city3,city4,city5.我有一个列表my_cities = ["city1","city3","city10"].我想根据my_cities中的列对df进行子集设置.
I have a pandas dataframe df with columns city1, city2,city3,city4,city5. I have a list my_cities = ["city1","city3","city10"]. I want to subset df according to the columns in my_cities.When I do,
my_cities = ["city1","city3","city10"]
my_cities = ["city1","city3","city10"]
df_my_cities = df [my_cities]
df_my_cities = df[my_cities]
我收到错误KeyError:"['city10']不在索引中"
I get the error KeyError: "['city10'] not in index"
如果my_cities中的元素不在df中,我如何告诉代码继续执行?
How can I tell the code to keep proceeding if an element from my_cities in not in 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
这篇关于子集pandas dataframe时忽略KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!