I wanna fill NaN cells
For example, if this is my DataFrame:
| name | id |
|---|---|
| Mike | 12 |
| Toby | 13 |
| Kevin | 14 |
| Toby | |
| Kevin | |
| Mike |
I would like to get this output:
| name | id |
|---|---|
| Mike | 12 |
| Toby | 13 |
| Kevin | 14 |
| Toby | 13 |
| Kevin | 14 |
| Mike | 12 |
How can I do it?
CodePudding user response:
Assuming NaNs in the empty cells (if not, first replace '' with pd.NA):
df['id'] = df.groupby('name')['id'].transform('first')
Or, to simply ffill:
df['id'] = df.groupby('name')['id'].ffill()
CodePudding user response:
Make sure you don't have any NaN or other values in id columns which will be max (or change max() to acc.)
df = pd.DataFrame(data=[['A','1'], ['B','2'], ['C','3'], ['C',''], ['B',''], ['A','']], columns=["name", "id"])
sub_df = df.groupby("name").apply(lambda row: row["id"].max())
df["id"] = df.apply(lambda row: sub_df[row["name"]], axis=1)
