将状态简化为缩写

2024-03-29 12:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试清理一个数据集,其中的状态要么是缩写,要么是完全拼出来的。我需要把它们都做成缩写。 有没有作弊的? 这就是我想到的,但我仍然没有得到正确的输出。我错过了什么

states = []
for c in by_state['order state']:
    if len(c)==2:
        states = c.upper()
    else:
        map(abbr.get,c)

Tags: 数据inmapforbylenif状态
2条回答

这里有一个方法。你知道吗

import re

"""Table to Map States to Abbreviations Courtesy https://gist.github.com/Quenty/74156dcc4e21d341ce52da14a701c40c"""
statename_to_abbr = {
    # Other
    'District of Columbia': 'DC',

    # States
    'Alabama': 'AL',
    'Montana': 'MT',
    'Alaska': 'AK',
    'Nebraska': 'NE',
    'Arizona': 'AZ',
    'Nevada': 'NV',
    'Arkansas': 'AR',
    'New Hampshire': 'NH',
    'California': 'CA',
    'New Jersey': 'NJ',
    'Colorado': 'CO',
    'New Mexico': 'NM',
    'Connecticut': 'CT',
    'New York': 'NY',
    'Delaware': 'DE',
    'North Carolina': 'NC',
    'Florida': 'FL',
    'North Dakota': 'ND',
    'Georgia': 'GA',
    'Ohio': 'OH',
    'Hawaii': 'HI',
    'Oklahoma': 'OK',
    'Idaho': 'ID',
    'Oregon': 'OR',
    'Illinois': 'IL',
    'Pennsylvania': 'PA',
    'Indiana': 'IN',
    'Rhode Island': 'RI',
    'Iowa': 'IA',
    'South Carolina': 'SC',
    'Kansas': 'KS',
    'South Dakota': 'SD',
    'Kentucky': 'KY',
    'Tennessee': 'TN',
    'Louisiana': 'LA',
    'Texas': 'TX',
    'Maine': 'ME',
    'Utah': 'UT',
    'Maryland': 'MD',
    'Vermont': 'VT',
    'Massachusetts': 'MA',
    'Virginia': 'VA',
    'Michigan': 'MI',
    'Washington': 'WA',
    'Minnesota': 'MN',
    'West Virginia': 'WV',
    'Mississippi': 'MS',
    'Wisconsin': 'WI',
    'Missouri': 'MO',
    'Wyoming': 'WY',
}


def multiple_replace(lookup, text):
  """Perform substituions that map strings in the lookup table to valuees (modification from https://stackoverflow.com/questions/15175142/how-can-i-do-multiple-substitutions-using-regex-in-python)"""
  # re.IGNORECASE flags allows provides case insensitivity (i.e. matches New York, new york, NEW YORK, etc.)
  regex = re.compile(r'\b(' + '|'.join(lookup.keys()) + r')\b', re.IGNORECASE)


  # For each match, look-up corresponding value in dictionary and peform subsstituion
  # we convert match to title to capitalize first letter in each word
  return regex.sub(lambda mo: lookup[mo.string.title()[mo.start():mo.end()]], text) 

if __name__ == "__main__": 

  text = """United States Census Regions are:
Region 1: Northeast
Division 1: New England (Connecticut, Maine, Massachusetts, New Hampshire, Rhode Island, and Vermont)
Division 2: Mid-Atlantic (New Jersey, New York, and Pennsylvania)
Region 2: Midwest (Prior to June 1984, the Midwest Region was designated as the North Central Region.)[7]
Division 3: East North Central (Illinois, Indiana, Michigan, Ohio, and Wisconsin)
Division 4: West North Central (Iowa, Kansas, Minnesota, Missouri, Nebraska, North Dakota, and South Dakota)"""

  print(multiple_replace(statename_to_abbr, text))

输出示例

United States Census Regions are:Region 1: NortheastDivision 1: New England (CT, ME, MA, NH, RI, and VT)Division 2: Mid-Atlantic (NJ, NY, and PA)Region 2: Midwest (Prior to June 1984, the Midwest Region was designated as the North Central Region.)[7]Division 3: East North Central (IL, IN, MI, OH,and WI)Division 4: West North Central (IA, KS, MN, MO,NE, ND, and SD)

谢谢你的帮助。我最终找到了我的用例的所有答案,所以这里是我所需要的,以防其他人需要它。你知道吗

#Creates new dataframe with two columns,removes all the NaN values
by_state = sales[['order state','total']].dropna()

#Map a dictionary of abbreviations to the dataframe
by_state['order state'] = by_state['order state'].map(abbr).fillna(by_state['order state'])

#Map values that were not capitalized correctly
by_state['order state'] = by_state['order state'].apply(lambda x:x.title()).map(abbr).fillna(by_state['order state'])

#Convert all abbreviations to uppercase
by_state['order state'] = by_state['order state'].apply(lambda x:x.upper())

#Remove a period after a abbreviation
by_state['order state'] = by_state['order state'].apply(lambda x:x.split('.')[0])

相关问题 更多 >