简体   繁体   English

如何使用python中另一列的值创建一个新列

[英]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.

相关问题 从Python中的另一列创建新列 - Create a new column from another column in Python 如何根据另一列的最小值创建新列 - How to create a new column based on minimum value from another column 在python中:如何使用括号中的列值和另一个值创建新列。 新列= column1(column2) - In python: how to create new column with value of column and value of another between brackets. New column= column1(column2) Python:在数据帧中,创建一个新列,并使用从另一列的值中切出的字符串 - Python: In a dataframe, create a new column with a string sliced from a column with the value of another column 如何创建一个新列,在Python中的某些条件下从另一个表返回值 - How to create a new column that returns value from another table under certain criteria in Python Python Pandas 根据另一个列值创建新列 - Python Pandas create new column based on another column value 根据列表中的另一列内容创建新的列值 - Create a new column value based on another column content from a list 创建新的数据框列,保留另一列的第一个值 - Create new dataframe column keeping the first value from another column Python Pandas 旋转:如何在第一列中分组并为第二列中的每个唯一值创建一个新列 - Python Pandas pivoting: how to group in the first column and create a new column for each unique value from the second column 如何在 Pandas 中创建新列,条件是重复另一列的值? - How to create new column in Pandas with condition to repeat by a value of another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM