简体   繁体   English

在检查列值是否包含作为列表中元素的字符串后,如何将列表中的元素分配给数据框列? (Python)

[英]How to assign element from a list to a dataframe column after checking if a column value contains a string that is an element in the list? (Python)

I have a pandas dataframe with a 'state' column that contains a string indicating a US state, however some of the records have the state name next to the abbreviation and others have just the abbreviation (eg some have 'Florida - FL' and others just 'FL').我有一个带有“州”列的熊猫数据框,其中包含一个指示美国州的字符串,但是有些记录在缩写旁边有州名,而其他记录只有缩写(例如,有些有“佛罗里达 - FL”和其他只是'FL')。 I want to check whether the string in the 'state' column contains an element from the following list of state abbreviations:我想检查“状态”列中的字符串是否包含以下状态缩写列表中的元素:

state_abbrevs = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA", 
          "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", 
          "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", 
          "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", 
          "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]

and afterwards assign whatever said element is to a new column (for the purposes of this question the new column is called 'state_std').然后将所说的任何元素分配给一个新列(对于这个问题,新列称为“state_std”)。 I do not want to do this by looping through rows.我不想通过循环遍历行来做到这一点。 How would I accomplish this?我将如何做到这一点?

This question is identical to the question here: Check if column contains value from a list and assign that value to new column此问题与此处的问题相同: 检查列是否包含列表中的值并将该值分配给新列

except that the above question is about how to do this in R, not Python.除了上述问题是关于如何在 R 中执行此操作,而不是 Python。

Let's assume that the abbreviated state name is always at the end of the string.让我们假设缩写的州名总是在字符串的末尾。 How about this?这个怎么样?

state_abbrevs = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA", 
          "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", 
          "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", 
          "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", 
          "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"] 
                 
def state_parser(state):
    state_std = next((abbr for abbr in state_abbrevs if state.endswith(abbr)),None)
    if state_std:
        return state_std
    else:
        return state

data = ["Florida - FL", "NY", "California - CA"]

df = pd.DataFrame(data, columns=['state'])
df['state_std'] = df['state'].apply(state_parser)
print(df)

Output:输出:

             state state_std
0     Florida - FL        FL
1               NY        NY
2  California - CA        CA

If the abbreviation doesn't always happen to be at the end, you can change the code:如果缩写并不总是出现在末尾,您可以更改代码:

state_std = next((abbr for abbr in state_abbrevs if abbr in state),None)

暂无
暂无

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

相关问题 将 dataframe 中的每个字符串元素与一个列表进行比较,并将其分配给一个列 python pandas - Compare each string element in a dataframe to a list and assign it to a column, python pandas 检查字符串是否包含Python中列表中的元素时出错? - Error while checking if a string contains an element from a list in Python? 如何测试字符串包含列表中的元素,并通过Pandas将目标元素分配给另一列 - How to test string contains elements in list and assign the target element to another column via Pandas 根据列中的值从 pandas dataframe 中的列表中删除元素 - Remove element from list in pandas dataframe based on value in column 检查列的任何字符串元素是否与python中的其他列字符串列表匹配 - Checking if any string element of the column is matching with other column string list in python 如何从列表创建单个列的DataFrame,其中第一个元素是python中的列名 - How to create a DataFrame of a single column from a list where the first element is the column name in python 检查数据框中的列是否包含字符串列表中的任何项目 - Checking if column in dataframe contains any item from list of strings 从 Pandas DataFrame 列中获取列表元素 - Get a list element from a Pandas DataFrame column 如何对 DataFrame 列中的列表元素进行排序 - How to sort a list element in a DataFrame column 如何将列表中的元素添加到 dataframe 作为保留顺序的列? - How to add element from list to a dataframe as column preserving the order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM