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 |
CodePudding user response:
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
CodePudding user response:
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()
