【问题标题】:Split one string column to multiple columns Pandas将一列字符串拆分为多列 Pandas
【发布时间】:2021-06-11 16:34:59
【问题描述】:

我有一个字符串列如下所示

*摘要 *:技术 * 时间 *:2021 年 6 月 * 附加细节 *:xxx 等

我想将这一列拆分为多个列,如下所示,但不知道如何实现这一目标

总结时间附加细节

技术 2021 年 6 月 xxx

谢谢

【问题讨论】:

  • Stackoverflow 不是免费的编码服务。您应该发送honest attempt at the solution,然后然后在必要时询问有关它的具体问题。

标签: python pandas dataframe split multiple-columns


【解决方案1】:

我会使用 .str 方法或应用带有 result_type='expand' 的自定义函数(后者未显示)

问题澄清后编辑:

## 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

【讨论】:

  • 嗨,乔迪,非常感谢。抱歉,我应该澄清我的问题,原始列的格式为 column name: xxxx column name2: xxxx etc
猜你喜欢
  • 2022-12-29
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
相关资源
最近更新 更多