[英]Python Pandas - Create a new df column based on matching criteria
I am attempting to create a new column in my pandas data frame(df). 我试图在我的pandas数据框(df)中创建一个新列。 The value for each row in this column (call it column_new) needs to look at a reference column (column_ref) that already exists in the data frame. 此列中每一行的值(称为column_new)需要查看数据帧中已存在的参考列(column_ref)。 I used the unique values in column_ref and assigned them as keys in a dictionary, d: 我在column_ref中使用了唯一值,并将它们分配为字典d中的键:
d = {'column_ref_value1': 'a',
'column_ref_value2': 'b',
'column_ref_value3': 'c',}
The values in dict(d) are values that I want to assign to to column_new in my data frame. dict(d)中的值是我要分配给数据框中的column_new的值。 Here is what I tried to no avail: 这是我无济于事的方法:
for i in df['column_ref']:
for k, v in d.items():
if k == i:
df['column_new'] = v
When I call my df I am seeing column_new populated with value 'c' in every row, and I am not sure why. 当我调用df时,我看到在column_new的每一行中都填充了值'c',但我不确定为什么。 I'm guessing my issue has to do with improper iterating through a pandas dataframe or series. 我猜我的问题与熊猫数据框或系列的不正确迭代有关。
Thanks in advance! 提前致谢!
您可以使用replace() :
df['column_new'] = df.replace({'column_ref': d})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.