熊猫系列拆分了n次



我想只用第一块空格来分割pandas.Series

pd.Series.str.split提供了一个n参数,根据内联帮助,它听起来应该指定要执行的拆分次数。(它在注释中说Both 0 and -1 will be interpreted as return all splits,但实际上并没有具体说明它的作用!

无论如何,它似乎不起作用:

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

似乎是一个错误;您需要为它指定pat,以便它尊重n的值:

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

这些是源代码中的行,表明如果None pat,它会忽略n

# 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()
...

编辑:在GitHub上报告

最新更新