在大白色空间的分裂熊猫

时间:2018-04-08 16:54:13

标签: python pandas text split

我需要将以下数据帧(单个列)拆分为三个大空格:

df =

0       boots 0330            on 31 mar          clp n...
1       tesco stores 6292     on 31 mar          clp n...
2       uniqlo                on 31 mar          clp n...

我需要:

                 column_0      column_1          column_2
0       boots 0330            on 31 mar          clp n...
1       tesco stores 6292     on 31 mar          clp n...
2       uniqlo                on 31 mar          clp n...

我尝试与partition分开:

df.str.partition(' on ')

但它给出了:

                                                     0      1                            2
0                                  boots 0330              on   31 mar          clp nan nan
1                                  tesco stores 6292       on   31 mar          clp nan nan
2                                  uniqlo                  on   31 mar          clp nan nan

错过了mar xx上的整个列。任何简单的解决方案?

1 个答案:

答案 0 :(得分:1)

我认为需要split正则表达式\s{2,} - 2 or more whitespacesadd_prefix

df = s.str.split('\s{2,}', expand=True).add_prefix('column_')
print (df)

            column_0   column_1  column_2
0         boots 0330  on 31 mar  clp n...
1  tesco stores 6292  on 31 mar  clp n...
2             uniqlo  on 31 mar  clp n...