简体   繁体   English

将一个字符串列拆分为多列 Pandas

[英]Split one string column to multiple columns Pandas

I have one string column looks like below我有一个字符串列如下所示

*Summary *: Technology * Time *: June 2021 * Additional Detail *: xxx etc *摘要*:技术*时间*:2021年6月*附加细节*:xxx等

I want to split this one column into multiple columns like below but not sure how I can achieve this我想将这一列分成多列,如下所示,但不知道如何实现这一点

Summary Time Additional Detail摘要时间附加细节

Technology June 2021 xxx技术 2021 年 6 月 xxx

Thanks谢谢

I would use either.str methods or apply a custom function with result_type='expand' (latter not shown)我将使用任一.str 方法或应用带有 result_type='expand' 的自定义 function (后者未显示)

Editing after question was clarified:澄清问题后进行编辑:

## given that original column is called 'original_col'
## and a regular pattern is followed.
for i, field in enumerate(df.original_col.values[0].split(' ')): 
    # iterates through first 'reference' cell [chunks of field:value pairs]
    current_field = field.split(':')[0] #field name
    current_series = df.original_col.str.split(' ').str[i].str.split(':')[-1]
    df[current_field]= current_series

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM