简体   繁体   中英

How to create a new column with a value from another column in python

I have a large excel sheet, and my data looks like this:

type        price
shoes        10 
clothes      20
shoes        30
clothes      40

I want to move the "30" and "40" value to a new column, and later on I will drop the existing row. I expect it will show up like this:

type        price     new price
shoes        10        30
clothes      20        40

how can I make it in python?

Thankyou

You can try this:

df['newprice'] = df.groupby('type')['price'].transform('max')
df = df.drop_duplicates(subset="type", keep="first")
print(df)

Output:

      type  price  newprice
0    shoes     10        30
1  clothes     20        40

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