Filling cell based on existing cells

groupby and bfill

Keep in mind the the 0 in groupby(0) refers to the column named 0. If your column has a different name, use that.

df.groupby(0).bfill()

       0         1     2
0  8A564  10616280  json
1  8A928       NaN  json
2  8A563  10616222  json
3  8A564  10616280  json
4  8A563  10616222  json
5  8A564  10616280  json
6  8B1BB  10982483  json
7  8A564  10616280  json

If the ordering of what is null doesn't lend itself to back filling, you can get the first non-null value.

df[1] = df.groupby(0)[1].transform('first')
df

       0         1     2
0  8A564  10616280  json
1  8A928       NaN  json
2  8A563  10616222  json
3  8A564  10616280  json
4  8A563  10616222  json
5  8A564  10616280  json
6  8B1BB  10982483  json
7  8A564  10616280  json

Tags:

Python

Pandas