用字典值替换字符串 [英] Replace String with Value of Dictionary

查看:294
本文介绍了用字典值替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将尽可能简化.我有一个DataFrame,其中包含按州列出的业务.有些国家是缩写,有些则不是.我想用缩写(例如:新泽西州新泽西州)替换全州名.

I will simplify as much as possible. I have a DataFrame with a list of businesses by state. Some States are abbreviated, some are not. I want to replace the full state name with the Abbreviation (ex: New Jersey to NJ).

我在此处找到了一个很酷的模块"US",其中列出了所有州及其州词典中的缩写.我想用缩写代替全名.

I found a cool module "US" found here that lists all the states and their abbreviations in a dictionary. What I would like to do is replace the full name with the abbreviations.

代码:

import pandas as pd
import numpy as np
import us
dfp = pd.DataFrame({'A' : [np.NaN,np.NaN,3,4,5,5,3,1,5,np.NaN], 
                    'B' : [1,0,3,5,0,0,np.NaN,9,0,0], 
                    'C' : ['Pharmacy of Oklahoma','NY Pharma','NJ Pharmacy','Idaho Rx','CA Herbals','Florida Pharma','AK RX','Ohio Drugs','PA Rx','USA Pharma'], 
                    'D' : [123456,123456,1234567,12345678,12345,12345,12345678,123456789,1234567,np.NaN],
                    'E' : ['Assign','Unassign','Assign','Ugly','Appreciate','Undo','Assign','Unicycle','Assign','Unicorn',]})
print(dfp)

statez = us.states.mapping('abbr', 'name')
lst_of_abbrv = statez.keys()
lst_of_states = statez.values()

phrase = "Pharmacy of Oklahoma"

for x in phrase.split():
    if x in lst_of_states:
        x= x.replace(x, 'State')
        print(phrase.split())

现在,我唯一能做的就是使用字符串并将其替换为"State"一词.如何用字典中的缩写替换名称?我尝试过并想要类似x= x.replace(x, lst_of_abbrv)的内容 但这会出错,因为您显然无法用dict_keys替换.

Right now the only thing I'm able to do is use a string and replace it with the word "State". How do i replace the name with the abbreviations from the dictionary? I've tried and want something like x= x.replace(x, lst_of_abbrv) but it errors because you obviously can't replace with dict_keys.

如果可以解释如何将其应用于数据框的"C"列,请加分

Extra points if you are able to explain how to apply this to column "C" of the Dataframe

推荐答案

首先,我将定义一个函数,该函数将替换字符串中状态的全名(如果存在)或返回原始字符串.

First I would define a function that would replace the full name of states in a string if any exist or return the original string.

def replace_states(company):
    # find all states that exist in the string
    state_found = filter(lambda state: state in company, statez.keys())

    # replace each state with its abbreviation
    for state in state_found:
        company = company.replace(state, statez[state])
    # return the modified string (or original if no states were found)
    return company

然后您可以将此功能应用于数据框的整个列

then you can apply this function to the entire column of the dataframe

dfp['C'] = dfp['C'].map(replace_states)

这篇关于用字典值替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆