我想从描述字段(我的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,其中包含匹配国家/地区的其他列
答案 0 :(得分:2)
如果我理解你,你需要这样的东西:
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"
我希望,它可以帮助你做你想做的事。