我需要将以下数据帧(单个列)拆分为三个大空格:
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上的整个列。任何简单的解决方案?
答案 0 :(得分:1)
我认为需要split
正则表达式\s{2,}
- 2 or more whitespaces
和add_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...