2017-03-08 9 views
1

ich Pandas Serie (benannt df) in folgendem Format haben:Pandas Serie extractall Fehler

     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 

ich alle Kurse extrahieren möchten ("[AZ] {4} \ s * \ d {5} "oder" \ d {5} ") aus Spalte1. Die gewünschte Datensatz wird in folgendem Format sein:

  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 

ich versuchte

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

und bekam die erste abgestimmte Muster.

versuchte ich

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

bekam aber den folgenden Fehler:

Length of names must match number of levels in MultiIndex. 

Wer hat eine Ahnung, was ich tun soll?

+0

können Sie Ihre gewünschte Datensatz aufgeben? – MaxU

+0

Ich habe es gerade gepostet. Vielen Dank! @MaxU – Claudia

Antwort

0

Wenn Sie eine ältere Version von Pandas verwenden, haben Sie vielleicht etwas wie this issue gefunden (obwohl Ihre Indizes nicht von der problematischen Form zu sein scheinen). In der Version 0.19.0 laufen beide Fälle ohne Fehler:

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

Try this:

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