3

Populating the column values from other rows based on the col1 values containing the names.

Hi, guys, forgive me for my poor English, I hope I'm able to explain my query properly. I've tried grouping by col1 but I'm confused about how to achieve the target. Please help!!

Input Dataframe:

col1    col2    col3     col4 
john    nan      65      Tokyo
peter   55       nan     Denver
Kia     23       90      nan
Faizu   45       nan     Ukraine
john    21       nan     nan 
peter   55       43      nan
Faizu   nan      76      Ukraine
Kia     nan      nan     Nairobi

Output Dataframe:

col1    col2    col3    col4
john     21      65     Tokyo
peter    55      43     Denver
Kia      23      90     Nairobi
Faizu    45      76     Ukraine
genz_on_code
  • 429
  • 4
  • 12

1 Answers1

4

Try:

df.groupby('col1', as_index=False).first()

Output:

    col1  col2  col3     col4
0  Faizu  45.0  76.0  Ukraine
1    Kia  23.0  90.0  Nairobi
2   john  21.0  65.0    Tokyo
3  peter  55.0  43.0   Denver
Scott Boston
  • 147,308
  • 15
  • 139
  • 187