Search code examples
pythonpandasdataframefor-looplookup

Lookup between two dataframes


I have a dataframe like below from an API response:

enter image description here

However, The API can give more variables like street, direction etc., So, I need to create an output from the API response which automatically define null. Example like below

enter image description here

For this I created a list of all the variables I want in the output and tried and match API response dataframe to the output dataframe.

Any help will be greatly appreciated

Thanks in advance


Solution

  • IIUC use DataFrame.reindex by list, if column not exist is filled by missing values:

    L = ['house','house_number',..., 'street', 'direction']
    df = df1.reindex(L, axis=1)