TransWikia.com

pandas dataframe groupby and fill with first row values

Stack Overflow Asked by pyd on December 23, 2021

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.

One Answer

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

Answered by Shubham Sharma on December 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP