python - 通过 str.contains() 建立索引,然后将值插入到另一列中

标签 python pandas

我有一个商店名称的数据框,我必须对其进行标准化。例如McDonalds 1234 LA -> McDonalds

import pandas as pd
import re

df = pd.DataFrame({'id': pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],dtype='int64',index=pd.RangeIndex(start=0, stop=10, step=1)), 'store': pd.Series(['McDonalds', 'Lidl', 'Lidl New York 123', 'KFC ', 'Taco Restaurant', 'Lidl Berlin', 'Popeyes', 'Wallmart', 'Aldi', 'London Lidl'],dtype='object',index=pd.RangeIndex(start=0, stop=10, step=1))}, index=pd.RangeIndex(start=0, stop=10, step=1))

print(df)

   id              store
0   1          McDonalds
1   2               Lidl
2   3  Lidl New York 123
3   4               KFC 
4   5    Taco Restaurant
5   6        Lidl Berlin
6   7            Popeyes
7   8           Wallmart
8   9               Aldi
9  10        London Lidl

假设我想要标准化 Lidl 商店。标准名称只是“Lidl”。

我想找到 Lidl 在数据框中的位置,并创建一个新列 df['standard_name'] 并在其中插入标准名称。但我无法弄清楚这一点。

我将首先创建将插入标准名称的列:

d['standard_name'] = pd.np.nan

然后搜索 Lidl 实例,并将清理后的名称插入到 standard_name 中。

首先计划是使用str.contains,然后将标准化值设置为新列:

df[df.store.str.contains(r'\blidl\b',re.I,regex=True)]['standard'] = 'Lidl'

print(df)

   id              store  standard_name
0   1          McDonalds       NaN
1   2               Lidl       NaN
2   3  Lidl New York 123       NaN
3   4               KFC        NaN
4   5    Taco Restaurant       NaN
5   6        Lidl Berlin       NaN
6   7            Popeyes       NaN
7   8           Wallmart       NaN
8   9               Aldi       NaN
9  10        London Lidl       NaN

尚未插入任何内容。我只检查了 str.contains 代码,发现它全部返回 false:

df.store.str.contains(r'\blidl\b',re.I,regex=True)

0    False
1    False
2    False
3    False
4    False
5    False
6    False
7    False
8    False
9    False
Name: store, dtype: bool

我不确定这里发生了什么。

我想要得到的是像这样填写的标准化名称:

   id              store  standard_name
0   1          McDonalds       NaN
1   2               Lidl       Lidl       
2   3  Lidl New York 123       Lidl       
3   4               KFC        NaN
4   5    Taco Restaurant       NaN
5   6        Lidl Berlin       Lidl       
6   7            Popeyes       NaN
7   8           Wallmart       NaN
8   9               Aldi       NaN
9  10        London Lidl       Lidl       

我将尝试标准化数据集中的大多数企业名称,麦当劳,汉堡王等。任何帮助表示赞赏

此外,这是最快的方法吗?有数百万行需要处理。

最佳答案

如果想设置新列,您可以使用 DataFrame.loccase=Falsere.I :

注意:d['standard_name'] = pd.np.nan不是必需的,可以省略。

df.loc[df.store.str.contains(r'\blidl\b', case=False), 'standard'] = 'Lidl'
#alternative
#df.loc[df.store.str.contains(r'\blidl\b', flags=re.I), 'standard'] = 'Lidl'
print (df)
   id              store standard
0   1          McDonalds      NaN
1   2               Lidl     Lidl
2   3  Lidl New York 123     Lidl
3   4               KFC       NaN
4   5    Taco Restaurant      NaN
5   6        Lidl Berlin     Lidl
6   7            Popeyes      NaN
7   8           Wallmart      NaN
8   9               Aldi      NaN
9  10        London Lidl     Lidl

或者可以使用另一种方法 - Series.str.extract :

df['standard'] = df['store'].str.extract(r'(?i)(\blidl\b)')
#alternative
#df['standard'] = df['store'].str.extract(r'(\blidl\b)', re.I)
print (df)
   id              store standard
0   1          McDonalds      NaN
1   2               Lidl     Lidl
2   3  Lidl New York 123     Lidl
3   4               KFC       NaN
4   5    Taco Restaurant      NaN
5   6        Lidl Berlin     Lidl
6   7            Popeyes      NaN
7   8           Wallmart      NaN
8   9               Aldi      NaN
9  10        London Lidl     Lidl

关于python - 通过 str.contains() 建立索引,然后将值插入到另一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59788913/

相关文章:

javascript - 将段落拆分成句子

python - 从 Python Social Auth 获取访问 token

python - import * 只允许在模块级别。使用 Pyscripter 的 Python 3.3

python - Django 和 Oracle DB 失去联系

python-3.x - 在具有相同 ID 的行末尾添加 Pandas 值

python - 支持 Nan 的 Pandas Lambda 函数

python - 比较两列,一列通过 float ,另一列通过字符串来获得匹配值

python - 在循环内使用 repl_python()

python - Pandas 数据透视表中的多维乘法

python - 在 pandas 中,如何选择包含 NaN 的行?