【问题标题】:Map dataframe function without lambda没有lambda的地图数据框功能
【发布时间】:2023-01-05 20:04:44
【问题描述】:

我有以下功能:

def summarize(text, percentage=.6):
    import numpy as np
    sentences = nltk.sent_tokenize(text)
    sentences = sentences[:int(percentage*len(sentences))]
    summary = ''.join([str(sentence) for sentence in sentences])
    return summary

我想将它映射到数据框行。当我使用以下代码时效果很好:

df['summary'] = df['text'].map(summarize)

但是,当我想在此调用中更改百分比变量时,它执行了 df['summary'] = df['text'].map(summarize(percentage=.8)),它显示了一个错误,表明它需要另一个参数,即 text。当然可以使用a来解决拉姆达功能如下:

df['summary'] = df['text'].map(lambda x: summarize(x, percentage=.8))

但我不想在通话中使用 lambda。有没有其他方法可以做到这一点?例如在函数内部使用 kwargs 来引用数据框中的 text 列?谢谢

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    利用:

    df['summary'] = df['text'].map(summarize, percentage=.8)
    

    或者:

    df['summary'] = df['text'].apply(summarize, percentage=.8)
    

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2021-12-30
      • 2022-01-07
      相关资源
      最近更新 更多