Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Merging dataframes in Pandas but dealing with 'non-existent' columns

$
0
0

I have a question regarding merging dataframes.

I have created a dataframe with three empty columns, named USD, EUR, GBP. 

  group   USD   EUR   GBP0     A  None  None  None1     B  None  None  None2     C  None  None  None

I now would like to fill in values for these columns, based on the USD, EUR, GBP values that I have saved in another dataframe, for example:

  group   USD   EUR   GBP0     A   NaN  0.05  0.041     B  0.04   NaN  0.032     C  0.02   NaN  0.01

Why am I not just merging based on 'group' ? Because the value dataframe would not always show three columns but two columns instead, for example:

  group   USD   EUR0     A   NaN  0.051     B  0.04   NaN2     C  0.02   NaN

The issue here is that I would like to see all three columns in the final output, regardless of whether data of a particular column exists or not. So the final output, using the third table as 'data table' should look like this:

  group   USD   EUR  GBP0     A   NaN  0.05  NaN1     B  0.04   NaN  NaN2     C  0.02   NaN  NaN

Do you have any ideas on how to tackle this? Any help would be much appreciated


Viewing all articles
Browse latest Browse all 23160

Trending Articles