1

I have a dataframe that has the following columns:

 id     index1    index2    value
 DivA     1         4        3
 DivA     3         6        4
 DivB     1         3        5
 DivB     5         7        3

On another dataframe I have the following:

 id      index
DivA       1
DivA       2
      ...
DivA       10
DivB       1
      ...

final result: df2:

 id      index    value
DivA       1        3
DivA       2       nan (because no value in df1)
DivA       3        4
DivA       4        3
      ...
DivA       10       nan (because no value in df1)
DivB       1        5
      ...

I'm wondering if I can add a new column 'value' to the second df in a way that 'index1' and 'index2' are combined and that 'value' in the second df can have 'nan' if there is no values in the first dataframe 'value' ?

Thank you for help !

2
  • check with df.melt('id') Commented Sep 24, 2020 at 19:57
  • it puts the three columns into one, what I'm looking for is that I transform the 'index1' and 'index2' into 'index' where the missing values in the two columns have a nan, any thoughts ? Commented Sep 24, 2020 at 20:11

1 Answer 1

2
  #pd.melt first dataframe
    df3=pd.melt(df, id_vars=['id','value'], value_vars=['index1', 'index2'], value_name='index').drop(columns=['variable'])

#pd.concat second dataframe to the pd.met result, sort-values and drop duplicates in index 
(pd.concat([df3, df1], ignore_index=True).sort_values(by=['id','index'])).drop_duplicates(subset=['index'],keep='first')



     id  value  index
0   DivA    3.0      1
9   DivA    NaN      2
1   DivA    4.0      3
4   DivA    3.0      4
5   DivA    4.0      6
10  DivA    NaN     10
3   DivB    3.0      5
7   DivB    3.0      7
Sign up to request clarification or add additional context in comments.

1 Comment

It works, I found out that pd.melt can very usefull in a lot of tasks but need a bit of time to understand the concept, thank you !

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.