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