How can I fill empty cells

Question:

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?

Asked By: 아이울

||

Answers:

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()
Answered By: mozway

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)
Answered By: Azhar Khan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.