Is it possible to fill dataframe rows based on values from above rows? I want to do an Excel VLOOKUP on the top rows of the dataframe
Question:
Here is a example of what i mean.
Code Name Value
1 Fred 2
2 Gary 2
1
2
I want to fill name and value by his respective code. In this case, Fred in the third row and Gary in the fourth row. Regards.
Answers:
Try with groupby
with ffill
df.update(df.groupby('Code').ffill())
df
Out[94]:
Code Name Value
0 1 Fred 2.0
1 2 Gary 2.0
2 1 Fred 2.0
3 2 Gary 2.0
Here is a example of what i mean.
Code Name Value
1 Fred 2
2 Gary 2
1
2
I want to fill name and value by his respective code. In this case, Fred in the third row and Gary in the fourth row. Regards.
Try with groupby
with ffill
df.update(df.groupby('Code').ffill())
df
Out[94]:
Code Name Value
0 1 Fred 2.0
1 2 Gary 2.0
2 1 Fred 2.0
3 2 Gary 2.0