简体   繁体   English

用选定的列表值填写 dataframe 中的列

[英]Fill in column in dataframe with the selected list value

I have a lot of addresses in excel file.我在 excel 文件中有很多地址。 I have import it and store it in dataframe.我已将其导入并存储在 dataframe 中。 Now I want to detect the states in each address and show it in new column.现在我想检测每个地址中的状态并将其显示在新列中。 How do I loop for every row in my dataframe and add the value of the states in that row?如何循环我的 dataframe 中的每一行并添加该行中的状态值?

List of all states:所有状态列表:

allstates=['SELANGOR','JOHOR','KELANTAN','MALACCA','NEGERI SEMBILAN','PAHANG','PENANG','PERAK','PERLIS',
          'SABAH','SARAWAK','TERENGGANU','KUALA LUMPUR','K. LUMPUR','LABUAN','PUTRAJAYA']

and below is how I want my dataframe to be:以下是我希望我的 dataframe 的样子:

Address                             |    States
-------------------------------------------------------
311 Jalan Springhill SELANGOR       |    *SELANGOR*
31 Jalan Segamat JOHOR              |    *JOHOR*

I want the states (example:SELANGOR) to insert in the states column我希望将状态(例如:SELANGOR)插入状态列

Try this:尝试这个:

df['States'] = df.Address.str.extract('({})'.format('|'.join(allstates)))

If you are certain (or want) that the state names appear only at the end of the addresses:如果您确定(或希望)state 名称仅出现在地址的末尾:

df['Sates'] = df.Address.str.extract('({})$'.format('|'.join(allstates)))

Output: Output:

                         Address     Sates
0  311 Jalan Springhill SELANGOR  SELANGOR
1         31 Jalan Segamat JOHOR     JOHOR
import pandas as pd
data = pd.read_csv('states.csv')
print(data)
                         Address
0  311 Jalan Springhill SELANGOR
1         31 Jalan Segamat JOHOR

for index, row in data.iterrows():
    value = row.Address
    State = value.split()[-1:][0]
    data.loc[index,'State'] = State

print(data)
                         Address     State
0  311 Jalan Springhill SELANGOR  SELANGOR
1         31 Jalan Segamat JOHOR     JOHOR

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

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