Kevin Nash
Kevin Nash

Reputation: 1561

Pandas - KeyError as column does not exist in the output

I expect the final DataFrame to have the below structure:

col_a, col_b, col_c, col_d, col_e

However based on the data pulled from the API, I see the below columns having data

col_a, col_c, col_d

This results in a Keyerror as col_b and col_e are not in the data obtained from the API. Is there a way I can create empty columns if there is no data available based on the data obtained from the API.

Upvotes: 1

Views: 1855

Answers (1)

jezrael
jezrael

Reputation: 863731

Use DataFrame.reindex by all expected columns names in list, if some column missing is added filled by default by missing values:

print (df)
   col_a  col_c  col_d
0      1      2      3

expected = ['col_a', 'col_b', 'col_c', 'col_d', 'col_e']
df = df.reindex(expected, axis=1)
print (df)
   col_a  col_b  col_c  col_d  col_e
0      1    NaN      2      3    NaN

Upvotes: 2

Related Questions