我想从描述字段(我的DF中的列)中提取与字典中的另一个字符串匹配的第一个字符串(使用Python),如果没有匹配,则显示Null,例如:
# read an excel with columns (IDX, DESCRIPTION)
df = pd.read_excel("example.xlsx")
输入: example.xlsx
[IDX] [Column DESCRIPTION]
[Row 1] ["I live in Russia"]
[Row 2] ["I was borned in USA"]
[Row 3] ["I would like to move to England"]
我的字典有以下国家/地区:
countries= [
{'value': ['usa'], 'country': 'United States of America'},
{'value': ['u.s.a.'], 'country': 'United States of America'},
{'value': ['united states'], 'country': 'United States of America'},
{'value': ['spain'], 'country': 'Spain'},
{'value': ['russia'], 'country': 'Russia'},
{'value': ['rusia'], 'country': 'Russia'},
{'value': ['canada'], 'country': 'Canada'},
{'value': ['france'], 'country': 'France'},
{'value': ['mexico'], 'country': 'Mexico'}
]
输出:
[IDX] [Column DESCRIPTION] [Column Country]
[Row 1] ["I live in Russia"] ['RUSSIA']
[Row 2] ["I was borned in USA"] ['UNITED STATES OF AMERICA']
[Row 3] ["I would like to move to England"] [Null]
具有附加列的新excel,该列与匹配的国家/地区或空
发布于 2018-06-04 23:21:02
如果我没理解错的话,你想要这样的东西:
strngs = ["I live in Russia", "I was borned in USA", "I would like to move to England"]
dictt = ["USA", "CANADA", "RUSSIA", "MEXICO"]
for strng in strngs:
matched = False
for key in dictt:
if key in strng.upper():
print key
matched = True
if not matched:
print "Null"
我希望,它能帮助你做你想做的事情。
https://stackoverflow.com/questions/50683701
复制相似问题