Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to insert a 1 row into a dataframe for every ID

I have df that contains purchase data. Each row has a different item in the item column, but naturally the purchase id’s in purchaseId stays the same across the same purchase. How can I insert a row for each purchaseId that contains the value in the age column as seen in df2 but in the item column?

df

purchaseId     item      age  
    22         apples    35
    22         coffee    35
    22         wipes     35
    53        tomatoes   23
    53         sugar     23
    53         tea       23


df2

purchaseId     item      age  
    22         apples    35
    22         coffee    35
    22         wipes     35
    22         35        35
    53        tomatoes   23
    53         sugar     23
    53         tea       23
    53         23        23

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use concat with new DataFrame created by DataFrame.drop_duplicates and last sorting index by DataFrame.sort_index:

#necesary default index
#df = df.reset_index(drop=True)

df2 = (pd.concat([df, 
                  df.drop_duplicates('purchaseId', keep='last')
                    .assign(item = lambda x: x['age'])])
         .sort_index(kind='mergesort', ignore_index=True))
print (df2)
   purchaseId      item  age
0          22    apples   35
1          22    coffee   35
2          22     wipes   35
3          22        35   35
4          53  tomatoes   23
5          53     sugar   23
6          53       tea   23
7          53        23   23
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading