[英]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:我有一个很大的 excel 表,我的数据如下所示:
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.我想将“30”和“40”值移动到一个新列,稍后我将删除现有行。 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?我怎样才能在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.