简体   繁体   English

如何使用多个条件在 Pandas 中添加新列

[英]How to add new column in Pandas using multiple conditions

I have a dataframe that looks like this:我有一个看起来像这样的数据框:

destination_zip destination_state
502111387        IA
388588179        MS           
T2A2L9           AB                  
891              AUK     
774653028        TX   

I am trying to write a code that will be adding a new column as destination_country to my dataframe, something like this:我正在尝试编写一个代码,将一个新列作为destination_country添加到我的数据框中,如下所示:

destination_zip destination_state  destination_country
502111387        IA                 US
388588179        MS                 US 
T2A2L9           AB                 CA
891              AUK                NZ
774653028        TX                 US

what I have tried so far is:到目前为止我尝试过的是:

df.loc[df['destination_state']=='TX', df['destination_country']]= 'US'
df.loc[df['destination_state']=='IA', df['destination_country']]= 'US'
df.loc[df['destination_state']=='MS', df['destination_country']]= 'US'
df.loc[df['destination_state']=='AUK', df['destination_country']]= 'NZ'
df.loc[df['destination_state']=='AB', df['destination_country']]= 'CA'

but this is not way too long to work with, I wanted something that would be based on multiple conditions in a single line of code, something like this:但这不会太长,我想要在一行代码中基于多个条件的东西,如下所示:

df.loc[df['destination_state']=='TX','IA','MS' , df['destination_country']]= 'US'

but this code is not working, can anyone help me with this?但是这段代码不起作用,有人可以帮我吗? My dataframe has 7k rows, that's why I wanted something with multiple conditons.我的数据框有 7k 行,这就是为什么我想要具有多个条件的东西。 I am using juypter notebook, python-3我正在使用 juypter 笔记本,python-3

Here is how I would proceed:这是我将如何进行:

# this format is easy to maintain
countries = {'US': ['IA', 'MS', 'TX'],
             'CA': ['AB'],
             'NZ': ['AUK'],
             }

# transform it to the inverse
states = {v:k for k,vals in countries.items() for v in vals}

# map states -> country
df['destination_country'] = df['destination_state'].map(states)

output:输出:

  destination_zip destination_state destination_country
0       502111387                IA                  US
1       388588179                MS                  US
2          T2A2L9                AB                  CA
3             891               AUK                  NZ
4       774653028                TX                  US

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM