0

The dataframe is empty the first time the program is run. How should filtering be done on a dataframe, when accessing a non-existent column.

Is there a more graceful solution ?

Because I have to add a lot of 'try excepts'.

condition = (df1['a'] == '123')
try:
  df2 = df1.loc[condition]
except:
   df2 = pd.DataFrame()

I could do

if 'a' in df1.columns

I was hoping there is a solution where the dataframe returns an empty result without throwing an error.

ender maz
  • 91
  • 1
  • 8

0 Answers0