【问题标题】:Adding columns in Pandas depending on length of array?根据数组的长度在 Pandas 中添加列?
【发布时间】:2020-04-25 07:52:03
【问题描述】:

给定一个句子“你好。我正在写一个问题”,我想在标点符号上拆分句子并将每个子句添加到 Pandas 中的单独列中,例如:

dummy = ['Hello', 'I am writing a question']
pd.DataFrame(dummy).T

并且我希望迭代地执行此操作,例如:

for subsentence in sentence:
    subsentence = re.split(r'[.|,|?|!]', str(subsentence))
    df['item'] = subsentence

但这确实会产生:

【问题讨论】:

    标签: python pandas nlp


    【解决方案1】:

    将列表推导与展平一起使用:

    sentence = ["Hello. I am writing a question", "Hello. I am writing a question"]
    
    L = [x for subsentence in sentence for x in re.split(r'[.|,|?|!]', str(subsentence))]
    
    df = pd.DataFrame({'item':L})
    print (df)
                           item
    0                     Hello
    1   I am writing a question
    2                     Hello
    3   I am writing a question
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 2017-11-04
      • 1970-01-01
      • 2016-02-03
      相关资源
      最近更新 更多