简体   繁体   中英

pandas dataframe groupby and fill with first row values

I have a df like this,

df = pd.DataFrame({
    "Name" : ["A","B","C","D","E","F","G"],
    "part number" : ["1","3","2","1","5","1","2"],
    "detail1" : ["A","C","B","B","E","E","E"],
    "detail2" : ["one","three","two","two","five","five","five"]
})


df
Name    part number detail1 detail2
A           1           A   one
B           3           C   three
C           2           B   two
D           1           B   two
E           5           E   five
F           1           E   five
G           2           E   five

I would like to groupby Part Number and fill detail1 and detail2 with first row values.

My expected output,

Name    part number detail1 detail2
A       1           A       one
B       3           C       three
C       2           B       two
D       1           A       one
E       5           E       five
F       1           A       one
G       2           B       two

I tried, df.groupby("part number")[["detail1","detail2"]].first() but not giving the expected output, please help.

Use groupby on part number and transform column detail1 , detail2 using first and assign this transformed columns back to df :

cols = ['detail1', 'detail2']
df[cols] = df.groupby('part number')[cols].transform('first')

Result:

print(df)
  Name part number detail1 detail2
0    A           1       A     one
1    B           3       C   three
2    C           2       B     two
3    D           1       A     one
4    E           5       E    five
5    F           1       A     one
6    G           2       B     two

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