簡體   English   中英

通過 str.contains() 索引,然后將值插入另一列

[英]Indexing by str.contains(), then inserting a value into another column

我有一個必須標准化的商店名稱數據框。 例如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.loc with case=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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM