使用pandas与字符串列表交叉引用多列的有效方法

问题描述 投票:2回答:1

我需要在数据框文本列中搜索国家/地区名称或大写名称,然后将匹配项保存在新列中。我目前的解决方案是有效的,但需要很长时间。我想知道是否有可能使这更有效,理想情况是以矢量化的方式。

国家和首都列表存储在单独的countries数据框中。

我的主要数据框df

    date             text
0   2016-01-01       Bla bla bla bla
1   2016-01-01       Blu blu Nigeria
2   2016-01-01       Hey ho Norway
3   2016-01-01       This is text Paris
4   2016-01-01       Lorem lorem ipsum

countries数据帧:

    name             capital
0   France           Paris
1   Germany          Berlin
2   Norway           Oslo
3   China            Beijing

我目前的解决方案

def extract_countries(row):
    matches = []
    for country, adj in countries[['name', 'capital']].values:
        if any([country in row.text, adj in row.text]):
            matches.append(country)
    return ', '.join(matches)

df['countries'] = df.apply(extract_countries, axis=1)

期望的结果:

    date             text                      countries
0   2016-01-01       Bla bla bla bla           NaN
1   2016-01-01       Blu blu Nigeria           Nigeria
2   2016-01-01       Hey ho Norway             Norway
3   2016-01-01       This is text Paris        France
4   2016-01-01       Lorem lorem ipsum         NaN
5   2016-01-01       Germany attacked Benin    Germany, Benin
python pandas vectorization
1个回答
1
投票

这是一种方式。注意NaN(“非数字”)不适用于字符串列,所以我留下了没有找到匹配项的空字符串。

import pandas as pd

df = pd.DataFrame([['2016-01-01', 'Bla bla bla bla'], ['2016-01-01', 'Blu blu Nigeria'],
                   ['2016-01-01', 'Hey ho Norway'], ['2016-01-01', 'This is text Paris'],
                   ['2016-01-01', 'Lorem lorem ipsum']], columns=['date', 'text'])

countries = pd.DataFrame([['France', 'Paris'], ['Germany', 'Berlin'], ['Norway', 'Oslo'],
                          ['China', 'Beijing']], columns=['name', 'capital'])

ctry_set = set(countries.name)
cap_set = set(countries.capital)

df['countries'] = df['text'].apply(lambda x: ', '.join(i for i in ctry_set if i in x))
df['capitals'] = df['text'].apply(lambda x: ', '.join(i for i in cap_set if i in x))

#          date                text countries capitals
# 0  2016-01-01     Bla bla bla bla                   
# 1  2016-01-01     Blu blu Nigeria                   
# 2  2016-01-01       Hey ho Norway    Norway         
# 3  2016-01-01  This is text Paris              Paris
# 4  2016-01-01   Lorem lorem ipsum                   
© www.soinside.com 2019 - 2024. All rights reserved.