wychen
wychen

Reputation: 129

How to merge rows of a dataframe where index is the same

Let's say I have a dataframe, where indexes are the same:

index date1 date2 date3
A      1     NaN  NaN
A     NaN    3    NaN
B      1     NaN  NaN
A     NaN    NaN  0

How can I transofrm it into a more concise dataframe:

index date1 date2 date3
A      1     3     0
B      1    NaN   NaN

Upvotes: 0

Views: 42

Answers (1)

Umar.H
Umar.H

Reputation: 23099

assuming index is your actual index, a groupby.first() will do.

df1 = df.groupby(level=0).first()
print(df1)
date1  date2  date3
index
A        1.0    3.0    0.0
B        1.0    NaN    NaN

Upvotes: 1

Related Questions