【问题标题】:an array of string to sentences dataframe pandas一个字符串数组到句子数据框熊猫
【发布时间】:2017-07-01 20:39:29
【问题描述】:

我的数据框看起来像

Phrase                                           Sentiment
[seri, escapad, demonstr, adag, good, goos]         1
[seri, escapad, demonstr, adag, good, goos]         2

当我使用命令df.dtypes

Phrase       object
Sentiment     int64
dtype: object

我需要获取如下数据框:

Phrase                                           Sentiment
seri escapad demonstr adag good goos            1
seri escapad demonstr adag good bad             2

我试过这个代码

df['Phrase'] = df[df.Phrase.map(lambda x: ','.join(x))]

我应该怎么做

【问题讨论】:

  • 您好,您可以将列表转换为字符串...但是..您想对 DF 做什么?
  • @Ika8 情绪分析

标签: python arrays pandas dataframe


【解决方案1】:

几乎是正确的——你多了一层df[。试试这个:

df['Phrase'] = df.Phrase.map(lambda x: ' '.join(x))

您已经返回了要用于替换 Phrase 的列,但随后您尝试按其值进行索引,这会产生错误。

【讨论】:

    【解决方案2】:

    我认为你非常接近。以下行应该可以解决问题:

    df['Phrase'] = df['Phrase'].map(lambda x: ' '.join(x))
    

    【讨论】:

      【解决方案3】:

      用','代替','

      你可以像这样不使用 lambda:

      df['Phrase'] = df.Phrase.apply(', '.join)
      

      使用 lambda:

      df['Phrase'] = df.Phrase.apply(lambda x: ', '.join(x))
      

      【讨论】:

        猜你喜欢
        • 2017-02-24
        • 2016-07-30
        • 2018-04-26
        • 2022-07-27
        • 1970-01-01
        • 2021-11-15
        • 2021-01-28
        • 2019-03-14
        • 1970-01-01
        相关资源
        最近更新 更多