2017-03-08 213 views
1

我有以下格式的熊貓系列(命名DF):熊貓系列extractall錯誤

     col1 
    a    GEOS 13100 
    b    MATH 13100-MATH 13200 
    c    MATH 19100-19200 
    d    SPAN 10300 or 20300 
    e    EGPT 10101-10102-10103 
    f    MOGK 10100/30100 
    g    PHSC 12600 must be taken before PHSC 12620 

我想提取所有課程(「[AZ] {4} \ S * \ d {5} 「或」\ d {5}「)。所需的數據集將在以下格式:

  col1  col2   col3   col4  col5 
a  GEOS 13100 
b  MATH 13100  -   MATH 13200 
c  MATH 19100  -   19200 
d  SPAN 10300  or   20300 
e  EGPT 10101  -   10102   -   10103 
f  MOGK 10100 /  30100 
g  PHSC 12600     PHSC 12620 

我試圖

df.col1.str.extract('(([A-Z]{4}\s*\d{5}?)|(\d{5}?)).*?(and|\-|or|\, or|\:|\/|\.|\;|\(|\s?)') 

,得到了第一匹配的模式。

我試圖

df.col1.str.extractall('(([A-Z]{4}\s*\d{5}?)|(\d{5}?)).*?(and|\-|or|\, or|\:|\/|\.|\;|\(|\s?)') 

而且得到了以下錯誤:

Length of names must match number of levels in MultiIndex. 

任何人有任何想法,我應該怎麼辦呢?

+0

你能張貼設置你想要的數據? – MaxU

+0

我剛發佈它。謝謝! @MaxU – Claudia

回答

0

如果您使用的是較早版本的熊貓,您可能遇到類似this issue(儘管您的索引似乎不是有問題的形式)。在0.19.0版本,這兩種情況下運行沒有錯誤:

In [25]: df = pd.DataFrame({'col1': ['SPAN 10300 or 20300', 'SPAN 10300 or 20301', 'MOGK 10100/30100', 'PHSC 12600 must be taken before PHSC 12620']}) 

In [26]: df.index = ['a', 'b', 'c', 'd'] 

In [27]: df 
Out[27]: 
            col1 
a       SPAN 10300 or 20300 
b       SPAN 10300 or 20301 
c       MOGK 10100/30100 
d PHSC 12600 must be taken before PHSC 12620 

In [28]: df.col1.str.extract('(([A-Z]{4}\s*\d{5}?)|(\d{5}?)).*?(and|\-|or|\, or|\:|\/|\.|\;|\(|\s?)') 
/usr/local/bin/ipython:1: FutureWarning: currently extract(expand=None) means expand=False (return Index/Series/DataFrame) but in a future version of pandas this will be changed to expand=True (return DataFrame) 
    #!/usr/local/bin/python3.5 
Out[28]: 
     0   1 2 3 
a SPAN 10300 SPAN 10300 NaN 
b SPAN 10300 SPAN 10300 NaN 
c MOGK 10100 MOGK 10100 NaN/
d PHSC 12600 PHSC 12600 NaN 

In [29]: df.col1.str.extractall('(([A-Z]{4}\s*\d{5}?)|(\d{5}?)).*?(and|\-|or|\, or|\:|\/|\.|\;|\(|\s?)') 
Out[29]: 
       0   1  2 3 
    match          
a 0  SPAN 10300 SPAN 10300 NaN  
    1   20300   NaN 20300 NaN 
b 0  SPAN 10300 SPAN 10300 NaN  
    1   20301   NaN 20301 NaN 
c 0  MOGK 10100 MOGK 10100 NaN /
    1   30100   NaN 30100 NaN 
d 0  PHSC 12600 PHSC 12600 NaN  
    1  PHSC 12620 PHSC 12620 NaN NaN 
0

試試這個:

In [172]: df.col1.str.extractall(r'(\w{4}\s\d{4,}|\d{4,}|[\-/]|or)').unstack().fillna('') 
Out[172]: 
       0 
match   0 1   2 3  4 
a  GEOS 13100 
b  MATH 13100 - MATH 13200 
c  MATH 19100 -  19200 
d  SPAN 10300 or  20300 
e  EGPT 10101 -  10102 - 10103 
f  MOGK 10100 /  30100 
g  PHSC 12600 or PHSC 12620