简体   繁体   English

如何 select 基于 pandas 中的多个条件每行一列

[英]How to select a column per row based on multiple conditions in pandas

I have the following dataframe called df:我有以下 dataframe 称为 df:

    Identifier   Name1   Name2     Country   Otherdata.......
0   N102314      BDH.A   0123      AUS
1   D19u248      DDF     DDF.X     DEN
2   J19j09f      XXG.X   XXG.DD    GER
3   Jd139jf      D07.SS  D07       SG
4   Jh39222      DEE     DEE.O     US
5   HH819jf      HHD.OH  HHD       MX
6   Jajh393      HXX     HXX.K     US  
7   DeeaJJd      MSS.O   DEX.O     US 

I want to create a new column called Name0 where I select a column per row based on the following conditions.我想创建一个名为 Name0 的新列,其中我 select 根据以下条件每行一列。

If Country == "US", ALWAYS select what's in Name1 for Name0.如果 Country == "US",则始终为 select Name1 中 Name0 的内容。

Otherwise, check which name contain a ".", and choose that item for Name0.否则,检查哪个名称包含“.”,并为 Name0 选择该项目。 if both of Name1 and Name2 contains a dot print the word NAMEERROR in Name0.如果 Name1 和 Name2 都包含一个点,则在 Name0 中打印单词 NAMEERROR。

So the final frame will look like this:所以最终的帧将如下所示:

    Identifier   Name1   Name2     Country  Name0      NOTES....... 
0   N102314      BDH.A   0123      AUS      BDH.A      #not US so chose the one with the "."
1   D19u248      DDF     DDF.X     DEN      DDF.X      #not US so chose the one with the "."
2   J19j09f      XXG.X   XXG.DD    GER      NAMEERROR  #not US and both contains ".", print NAMEERROR
3   Jd139jf      D07.SS  D07       SG       D07.SS     #not US so chose the one with the "."
4   Jh39222      DEE     DEE.O     US       DEE        #US so chose Name1
5   HH819jf      HHD.OH  HHD       MX       HHD.OH     #not US so chose the one with the "."
6   Jajh393      HXX     HXX.K     US       HXX        #US so chose Name1
7   DeeaJJd      MSS.O   DEX.O     US       MSS.O      #both contain "." but US so chose Name1

I was thinking it might look like for the first part我在想它可能看起来像第一部分

df['Name0'] = np.NaN
df['Name0'] = np.where(df['Country'].str.contains('US'),df['Name1'],df['Name0'])

but I don't know where to start for the rest of the condition.但我不知道从哪里开始为条件的 rest。

apply is handy here. apply在这里很方便。

def fix(country, n1, n2):
    if country == 'US':
        return n1
    else:
        if ('.' in n1) & ('.' in n2):
            return 'NAMERERROR'
        elif '.' in n1:
            return n1
        elif '.' in n2:
            return n2


df['Name0'] = df.apply(lambda x: fix(country=x['Country'],
                                     n1 = x['Name1'],
                                     n2 = x['Name2']), axis=1)

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

相关问题 如何根据多个条件根据前一行填充 pandas dataframe 列的行? - How to populate rows of pandas dataframe column based with previous row based on a multiple conditions? 根据多列条件选择熊猫数据框中的行 - Select rows in a pandas data frame based on multiple column conditions 如何根据每行中的条件将多个字符串添加到 pandas dataframe 中的列中? - How do I add multiple strings to a column in a pandas dataframe based on conditions in each row? Python Pandas:如何为数据帧的每一行选择两个相等的列 - Python Pandas : How to select two equal column per row of a dataframe Pandas:如何根据同一列中的两个条件来 select 行 - Pandas: how to select rows based on two conditions in the same column 如何根据 pandas 中一行的多列创建条件 - How to make conditions based on multiple columns of a row in pandas 如何根据多个条件将值保存到 select pandas 行中 - How to save value into select pandas rows based on multiple conditions 熊猫-如何根据多列的条件创建具有3个输出的列 - Pandas - How to create a column with 3 outputs based on conditions on multiple columns 如何使用 pandas 根据多列条件计算行数? - How to count rows based on multiple column conditions using pandas? "如何根据多个条件估计 Pandas 数据框列值的计数?" - How to estimate count for Pandas dataframe column values based on multiple conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM