简体   繁体   English

Python Pandas-根据匹配条件创建新的df列

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

相关问题 根据 Python 中的条件创建一个新列 - Create a new column based in criteria in Python 如何根据多个条件在 pandas df 中创建一个新列? - How to create a new column in a pandas df based on multiple conditions? Python - 基于多个条件和空检查的 Pandas 新 DF 列 - Python - Pandas new DF column based on multiple conditions and null checks python:根据列名条件创建多索引pandas DF - python: Create a multiindex pandas DF based on condition of column names 根据列条件从以前的 df 中提取值到新的 df - extract value from previous df to new df based on column criteria 熊猫:根据行中的多个条件,向DF添加带有随机数的新列 - Pandas: Adding a new column with random numbers to DF based on multiple criteria from row 根据 PYTHON 中的分组和条件创建新的 df 列 - create new df column based on grouping and conditions in PYTHON 如何使用随机辅音字符串创建新的 pandas df 列匹配另一列的字符串长度? - How to create new pandas df column matching string length of another column with random consonant strings? Pandas 如何根据条件使用 DF2 中列的值在 DF1 中创建新列 - Pandas how to create new colum in DF1 with values of column in DF2 based on conditions 根据条件向pandas df添加新列 - Adding new column to pandas df based on condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM