【问题标题】:How to add pandas text column one below other如何在其他下方添加熊猫文本列
【发布时间】:2023-02-03 21:50:46
【问题描述】:

我想要 pandas dataframe 列中的 3 个输入列,如 pros、cons 和 sentiment,它们是文本列

pros| cons |sentiment
_________________________
text| text | positive
text| text | negative

我想让列像

review|sentiment               
________________
pros|positive                   
pros|positive                     
pros|positive                   
pros|positive                  
cons|negative             
cons|negative               
cons|negative     

       

我想要一个新的专栏评论,其中包含带有情感值的专业栏文本,下面是带有情感值的反对文本 如何将初始数据框安排到熊猫的评论和情绪列中?

【问题讨论】:

  • 向其发布可测试的 df 样本和预期结果

标签: pandas dataframe csv text


【解决方案1】:

IIUC,使用meltsort_values

假设 df 是您的数据框:

out = (
       df.melt(id_vars="sentiment",
               value_vars=["pros", "cons"],
               value_name="review")
        .sort_values(by="sentiment", ascending=False)
        [['review', 'sentiment']]
      )
​

输出 :

print(out)

  review sentiment
0   text  positive
2   text  positive
1   text  negative
3   text  negative

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    • 2022-01-06
    • 2018-10-26
    相关资源
    最近更新 更多