1

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.

1 Answer 1

2

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.