简体   繁体   English

如何使用基于 2 列的多个条件在 pandas 中创建新列?

[英]How to use multiple conditions based on 2 columns to create the new column in pandas?

I have 2 dictionaries and i need to create the column based on several condition.我有 2 个字典,我需要根据几个条件创建列。

dict1 = {'100': BMW, '200': Audi, '300': 'VW'}   
dict2 = {'100': Mercedes, '200': Nissan, '300': 'Renault'}

df:东风:

class          Code
1              200
1              300
2              300
1              100
2              100   

I actually want to use dict1 when class is 1 and dict2 when class is 2我实际上想在 class 为 1 时使用 dict1,当 class 为 2 时使用 dict2

desired output would be like this:所需的 output 将是这样的:

  class            Code         Car
    1              200          Audi
    1              300          VW
    2              300          Renault
    1              100          BMW
    2              100          Mercedes

I could use.map if i had no condition but i am not sure what to use now:如果我没有条件,我可以使用.map 但我不确定现在该使用什么:

df['Car'] = df['Code'].map(dict1)

i tested with the follwing code我用以下代码进行了测试

import pandas as pd
dict1 = {'100': 'BMW', '200': 'Audi', '300': 'VW'}
dict2 = {'100': 'Mercedes', '200': 'Nissan', '300': 'Renault'}
df = pd.DataFrame({'Class':[1,1,2,1],'Code':['200','300','300','100']})
def f(row):
    if row['Class'] == 1:
        val = dict1[row['Code']]
    elif row['Class'] ==2:
        val = dict2[row['Code']]
    else:
        val = dict2[row['Code']]
    return val

df['Car']= df.apply(f,axis=1)

print(df)

it prints它打印

    Class Code     Car
0      1  200     Audi
1      1  300       VW
2      2  300  Renault
3      1  100      BMW

If it's just two dictionaries/classes:如果它只是两个字典/类:

# note that your dictionary has string key
df['Code'] = df.Code.astype(str)

df['car'] = np.where(df['class']==1,
                     df['Code'].map(dict1),
                     df['Code'].map(dict2) )

Output: Output:

   class Code       Car
0      1  200      Audi
1      1  300        VW
2      2  300   Renault
3      1  100       BMW
4      2  100  Mercedes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas:根据多列条件新建列 - Pandas: Create New Column Based on Conditions of Multiple Columns 熊猫-如何根据多列的条件创建具有3个输出的列 - Pandas - How to create a column with 3 outputs based on conditions on multiple columns 如何根据多个条件在 pandas df 中创建一个新列? - How to create a new column in a pandas df based on multiple conditions? 如何按多列分组并根据Python中的条件创建新列? - How to group by multiple columns and create a new column based on conditions in Python? 根据多个条件将现有列的值分配给 Pandas 中的新列 - Assign value of existing column to new columns in pandas based on multiple conditions 基于多个条件在 Pandas 数据框中创建一个新列 - Create a new column in pandas dataframe based on multiple conditions 根据多个其他列的条件新建 Python DataFrame 列 - Create new Python DataFrame column based on conditions of multiple other columns 如何基于现有列熊猫的多个条件创建新列 - How to create new column based on multiple conditions from existing column pandas 如何根据熊猫中其他列的条件创建新列 - How to create a new column based on conditions on other column in pandas 如何根据Pandas中条件的现有列创建两列? - How create two columns based on existing column with conditions in Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM