2014-02-28 46 views
3

可以說我有類似的代碼如下:Python的大熊貓刪除UserWarning,有效地循環

import pandas as pd 

df=pd.DataFrame({'Name': [ 'Jay Leno', 'JayLin', 'Jay-Jameson', 'LinLeno', 'Lin Jameson', 'Python Leno', 'Python Lin', 'Python Jameson', 'Lin Jay', 'Python Monte'], 
       'Class': ['Rat','L','H','L','L','H', 'H','L','L','Circus']}) 
df['status']='' 

pattern1=['^Jay(\s|-)?(Leno|Lin|Jameson)$','^Python(\s|-)?(Jay|Leno|Lin|Jameson|Monte)$','^Lin(\s|-)?(Leno|Jay|Jameson|Monte)$' ] 
pattern2=['^Python(\s|-)?(Jay|Leno|Lin|Jameson|Monte)$' ] 
pattern3=['^Lin(\s|-)?(Leno|Jay|Jameson|Monte)$' ] 

for i in range(len(pattern1)): 
    df.loc[df.Name.str.contains(pattern1[i]),'status'] = 'A' 

for i in range(len(pattern2)): 
    df.loc[df.Name.str.contains(pattern2[i]),'status'] = 'B' 

for i in range(len(pattern3)): 
    df.loc[df.Name.str.contains(pattern3[i]),'status'] = 'C' 

print (df) 

它打印:

C:\Python33\lib\site-packages\pandas\core\strings.py:184: UserWarning: This pattern has match groups. To actually get the groups, use str.extract. 
    " groups, use str.extract.", UserWarning) 
    Class   Name status 
0  Rat  Jay Leno  A 
1  L   JayLin  A 
2  H  Jay-Jameson  A 
3  L   LinLeno  C 
4  L  Lin Jameson  C 
5  H  Python Leno  B 
6  H  Python Lin  B 
7  L Python Jameson  B 
8  L   Lin Jay  C 
9 Circus Python Monte  B 

[10 rows x 3 columns] 

我的問題是我怎麼刪除錯誤和是否有辦法用更少的代碼更有效地循環?我知道有些東西叫做列表解析,但我對如何使用它們感到困惑。

我知道錯誤可能

pd.options.mode.chained_assignment = None 

回答

8

抑制使用non-capturing parentheses(?:...)

pattern1=['^Jay(?:\s|-)?(?:Leno|Lin|Jameson)$','^Python(?:\s|-)?(?:Jay|Leno|Lin|Jameson|Monte)$','^Lin(?:\s|-)?(?:Leno|Jay|Jameson|Monte)$' ] 
pattern2=['^Python(?:\s|-)?(?:Jay|Leno|Lin|Jameson|Monte)$' ] 
pattern3=['^Lin(?:\s|-)?(?:Leno|Jay|Jameson|Monte)$' ] 

的警告來自this code

if regex.groups > 0: 
     warnings.warn("This pattern has match groups. To actually get the" 
         " groups, use str.extract.", UserWarning) 

所以只要沒有團體,沒有任何警告。

+0

我在另一組代碼上試過這個,但得到了'錯誤:沒有重複' – ccsv

+0

一個顯示這個錯誤的可運行示例會非常有幫助。 – unutbu

+0

沒關係我修復它顯然你不能有'(:?i)'它必須是'(?i)' – ccsv