Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ignore KeyError while subsetting pandas dataframe

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"]

df_my_cities = df[my_cities]

I get the error KeyError: "['city10'] not in index"

How can I tell the code to keep proceeding if an element from my_cities in not in df?

like image 911
Ssank Avatar asked Jan 05 '23 02:01

Ssank


1 Answers

You can use intersection between all columns and list:

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

Sample:

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

Alternatively numpy.intersect1d:

df_my_cities = df[np.intersect1d(df.columns, my_cities)]
print (df_my_cities)
  city1 city3
0     s     f
1     e     g
like image 68
jezrael Avatar answered Jan 12 '23 20:01

jezrael