0

I try to combine two rows in dataframe into one

              ID           Value1         Value2    
0             ID_1           NaN            2           
1             ID_2           NaN            7    
2             ID_1           5             NaN   
3             ID_2           8             NaN      

The result should be the following

              ID           Value1         Value2    
1             ID_1           5              2     
2             ID_2           8              7      

Is it possible with a method of dataframe ?

0

3 Answers 3

1

via stack/unstack

df = df.set_index('ID').stack().unstack().reset_index()

Output:

     ID  Value1  Value2
0  ID_1     5.0     2.0
1  ID_2     8.0     7.0
Sign up to request clarification or add additional context in comments.

1 Comment

It's not working, I have this error ValueError: Index contains duplicate entries, cannot reshape
1

Use set_index() , apply() method and sorted() method:

newdf=df.set_index('ID').apply(lambda x : sorted(x,key=pd.isnull))

Finally use boolean masking and isna() method:

newdf=newdf[~newdf.isna().all(1)]

Now If you print newdf you will get your desired output:

       Value1   Value2
ID      
ID_1    5.0     2.0
ID_2    8.0     7.0

If needed use reset_index() method:

newdf=newdf.reset_index()

Output of above code:

    ID      Value1  Value2
0   ID_1    5.0     2.0
1   ID_2    8.0     7.0

2 Comments

If there are unequal amounts of NAN's in value1/value2 will it not drop all after sorting?
you are right....updated answer....btw thanks for noticing this @Nk03 :)
0

For this particular case you can also use groupby():

df = df.groupby('ID')['Value1','Value2'].sum().reset_index()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.