這不是最好的方法,但這是我到目前為止所做的:
我有這個例子df:
df = pd.DataFrame({
'City': ['I lived Los Angeles', 'I visited London and Toronto','the best one is Toronto', 'business hub is in New York',' Mexico city is stunning']
})
df
給出:
City
0 I lived Los Angeles
1 I visited London and Toronto
2 the best one is Toronto
3 business hub is in New York
4 Mexico city is stunning
我正在嘗試從嵌套 dic 匹配(不區分大小寫)城市名稱,并使用帶有 int 值的國家名稱創建一個新列以用于統計目的。
所以,這是我的嵌套 dic 作為國家和城市的參考:
country = { 'US': ['New York','Los Angeles','San Diego'],
'CA': ['Montreal','Toronto','Manitoba'],
'UK': ['London','Liverpool','Manchester']
}
我創建了一個函式,它應該從 df 中查找城市并將其與 dic 匹配,然后創建一個帶有國家名稱的列:
def get_country(x):
count = 0
for k,v in country.items():
for y in v:
if y.lower() in x:
df[k] = count 1
else:
return None
然后將其應用于df:
df.City.apply(lambda x: get_country(x.lower()))
我得到以下輸出:
City US
0 I lived Los Angeles 1
1 I visited London and Toronto 1
2 the best one is Toronto 1
3 business hub is in New York 1
4 Mexico city is stunning 1
預期輸出:
City US CA UK
0 I lived Los Angeles 1 0 0
1 I visited London and Toronto 0 1 1
2 the best one is Toronto 0 1 0
3 business hub is in New York 1 0 0
4 Mexico city is stunning 0 0 0
uj5u.com熱心網友回復:
這是基于您的功能的解決方案。我更改了變數的名稱,使其更具可讀性和易于理解。
df = pd.DataFrame({
'City': ['I lived Los Angeles',
'I visited London and Toronto',
'the best one is Toronto',
'business hub is in New York',
' Mexico city is stunning']
})
country_cities = {
'US': ['New York','Los Angeles','San Diego'],
'CA': ['Montreal','Toronto','Manitoba'],
'UK': ['London','Liverpool','Manchester']
}
def get_country(text):
text = text.lower()
count = 0
country_counts = dict.fromkeys(country_cities, 0)
for country, cities in country_cities.items():
for city in cities:
if city.lower() in text:
country_counts[country] = 1
return pd.Series(country_counts)
df = df.join(df.City.apply(get_country))
輸出:
City US CA UK
0 I lived Los Angeles 1 0 0
1 I visited London and Toronto 0 1 1
2 the best one is Toronto 0 1 0
3 business hub is in New York 1 0 0
4 Mexico city is stunning 0 0 0
解決方案基于Series.str.count
一個更簡單的解決方案是使用每個國家/地區Series.str.count計算以下正則運算式模式的出現次數(模式匹配or或)。使用與上述相同的設定:city1|city2|etccity1city2etc
country_patterns = {country: '|'.join(cities) for country, cities in country_cities.items()}
for country, pat in country_patterns.items():
df[country] = df['City'].str.count(pat)
為什么您的解決方案不起作用?
if y.lower() in x: df[k] = count 1 else: return None
您的函式未產生正確輸出的原因是,None如果在文本中未找到城市,您將回傳:未檢查其余國家和城市,因為該return陳述句自動退出該函式。
發生的情況是只US檢查城市,并且該行df[k] = 1(在本例中k = 'US')創建一個名為k填充值 1 的整個列。它不是為該行創建單個值,而是創建或修改整個列。使用apply時要更改單行或值(函式的輸入),所以不要直接更改函式內部的主DataFrame。
uj5u.com熱心網友回復:
您可以使用 lambda 函式來實作此結果,以檢查字串中是否包含每個國家/地區的任何城市,首先將以下城市名稱小寫country:
cl = { k : list(map(str.lower, v)) for k, v in country.items() }
for ctry, cities in cl.items():
df[ctry] = df['City'].apply(lambda s:any(c in s.lower() for c in cities)).astype(int)
輸出:
City US CA UK
0 I lived Los Angeles 1 0 0
1 I visited London and Toronto 0 1 1
2 the best one is Toronto 0 1 0
3 business hub is in New York 1 0 0
4 Mexico city is stunning 0 0 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/492566.html
下一篇:從C 中的字串中洗掉多余的空格
