FungusAmungus
FungusAmungus

Reputation: 77

How to replace NaN values with values from another row

I have a table

df = pd.DataFrame({'car': ['toyota', 'toyota', 'ford', 'ford'],
 'doors': [nan, 2.0, nan, 4.0],
 'seats': [2.0, nan, 4.0, nan]})

that looks like this:

car doors seats
toyota NaN 2
toyota 2 NaN
ford NaN 4
ford 4 NaN

I want to replace NaN with values from rows that match a value from a specific column (i.e car)

I want this:

car doors seats
toyota 2 2
ford 4 4

Upvotes: 1

Views: 152

Answers (2)

user7864386
user7864386

Reputation:

Another option is to use groupby_first method. first method skips NaN values by default.

out = df.groupby('car', as_index=False).first()

Output:

      car  doors  seats
0    ford    4.0    4.0
1  toyota    2.0    2.0

Upvotes: 2

Ashwiniku918
Ashwiniku918

Reputation: 281

Suppose Your Dataframe name is Cars_df, grouping and taking maximum value should work, like below

   Cars_df.groupby(['car'])['door','seat'].max().reset_index()

Upvotes: 0

Related Questions