2017-04-25 81 views
2

我有一個熊貓數據框df,列city1,city2,city3,city4,city5。我有一個列表my_cities = [「city1」,「city3」,「city10」]。我想到子集根據my_cities.When我做列DF,在子集熊貓數據框時忽略KeyError

my_cities = [ 「city1」, 「請分享幫助」, 「city10」]

df_my_cities = DF [my_cities]

我得到錯誤KeyError:「['city10']不在索引中」

如何判斷代碼是否繼續執行,如果my_cities中的元素不在df中?

回答

3

您可以使用所有列和list之間intersection

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