【问题标题】:How to extract single tag from tags row ? Python Pandas如何从标签行中提取单个标签?蟒蛇熊猫
【发布时间】:2020-05-12 04:38:45
【问题描述】:

3300 行

我需要制作新的单列,每行带有单个标签

【问题讨论】:

  • 除非绝对必要,否则不要将信息作为图像共享。请参阅:meta.stackoverflow.com/questions/303812/…。我们需要比这更多的信息。这些数据是什么,它来自哪里?您显然没有阅读 Pandas 文档,请阅读。

标签: python sql pandas dataframe analysis


【解决方案1】:

DataFrame.explode (pandas 0.25+) 与Series.str.stripSeries.str.splitTags 一起用于列表:

df1 = (df.assign(Tags = df['Tags'].str.strip('><').str.split('><'))
         .explode('Tags')
         .reset_index(drop=True))

【讨论】:

    【解决方案2】:

    你必须拆分然后分解你的数据框。

    df['Tags'] = df['Tags'].astype('str')
    
    for i in range(len(df)):
         df.at[i, 'Tags'] = df[i, df['Tags'].strip('><').split('><')]
    
    df.explode('Tags')
    

    【讨论】:

    • 您的解决方案没有运行
    猜你喜欢
    • 2018-12-13
    • 2023-03-30
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    相关资源
    最近更新 更多