简体   繁体   中英

Pandas:How to fill in value from the same column but different row

I have a requirement to fill out a column with values from same column but from different rows.

Example: Below is the data structure.

    fname   lname
0    bob    andy
1  manny  dorson
2    bob     NaN 

Now for all " lname " values which are NaN, I want to fill those with " lname " values from the rows which share the common " fname "

So 3rd row which does not have " lname " I want to pick the " lname " from 1st row since " fname " for both the rows are same. The result which I expect:

    fname   lname
0    bob    andy
1  manny  dorson
2    bob     andy 

This is just a simple minified example. And let's say, if there are multiple rows with matching first name, we can pick up the first one. I tried a lot of things but not getting it to work. Any help is appreciated. Thanks.

Use groupby and ffill :

#To fill the NaN's with last value of group
df['lname'] = df.groupby('fname', as_index=False)['lname'].ffill()

#To fill the NaN's with first value of group
df['lname'] = (df['lname'].fillna(df.groupby('fname')['lname']
                                    .transform('first')))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM