Pandas Series divided n times

I would like to divide pandas.Seriesthe first part of the spaces.

pd.Series.str.splitoffers a parameter nthat, according to the built-in help form, looks like this, should indicate how many sections to execute. (he speaks Both 0 and -1 will be interpreted as return all splitsin notes, but doesn’t really indicate what he is doing!)

In any case, it does not work:

>>> x = pd.DataFrame(['Split Once', 'Split Once As Well!'])
>>> x[0].str.split(n=1)
0               [Split, Once]
1    [Split, Once, As, Well!]
+4
source share
1 answer

this seems like a mistake; you need to specify patthat it takes into account the value n:

x[0].str.split( n=1, pat=' ' )

these are lines in the source code that shows that it ignores nif pat None:

# pandas/core/strings.py
def str_split(arr, pat=None, n=None):
    if pat is None:
        if n is None or n == 0:
            n = -1
        f = lambda x: x.split()
...

edit: tell github

+6
source

All Articles