gpt4 book ai didi

python - 来自 Pandas Dataframe 的文本

转载 作者:行者123 更新时间:2023-12-01 08:59:12 25 4
gpt4 key购买 nike

我有一个 Pandas 数据框,其中包含软糖、巧克力和薄荷糖销售量的单个事件。它们按周数进行汇总和排序。我现在将其翻译成文本,然后通过电子邮件发送,使用以下方法:

pd['text'] = 'In calendar week (' + pd['weeknumber'].map(str) + '), customers have bought ' + pd['gummibears'].map(str) + 'kg of gummibears, ' + pd['chocolate'].map(str) + 'kg of chocolate, as well as ' + pd['mint'].map(str) + 'kg of mints.'

理想情况下,结果会给出一个很好的文本来概述销售情况。然而,有可能已经售出 0kg,当然也会出现,看起来像这样:

>>> "In calendar week 25, customers have bought 0kg of gummibears, 25kg of chocolate, as well as 0kg of mints."
>>> "In calendar week 26, customers have bought 6kg of gummibears, 0kg of chocolate, as well as 2kg of mints."

这可行,但会让读者感到困惑。有没有一种优雅的方法可以过滤掉所有 0kg 的实例,而无需嵌套多个循环?最好,上面的结果看起来像这样:

>>> "In calendar week 25, customers have bought 25kg of chocolate."
>>> "In calendar week 26, customers have bought 6kg of gummibears, as well as 2kg of mints."

最佳答案

您可以使用 numpy.where 的自定义函数和 eq 创建的 bool 掩码(==),但对于一般解决方案,文本必须进行标准化:

df = pd.DataFrame({
'weeknumber':[1,2,3,4,5,6],
'gummibears':[7,8,9,4,0,0],
'chocolate': [0,3,5,0,1,0],
'mint': [5,3,0,9,2,0]
})


def kg_to_string(col):
return np.where(df[col].eq(0), '', ' ' + df[col].astype(str) + 'kg of '+ col +',')

start = 'In calendar week (' + df['weeknumber'].astype(str) + '), customers have bought'

#boolean mask if all columns are 0
mask = df[['gummibears','gummibears','mint']].eq(0).all(axis=1)
df['text'] = start + np.where(mask, ' nothing', kg_to_string('gummibears') +
kg_to_string('chocolate') +
kg_to_string('mint'))
#remove last ,
df['text'] = df['text'].str.rstrip(',')
print (df['text'].tolist())
['In calendar week (1), customers have bought 7kg of gummibears, 5kg of mint',
'In calendar week (2), customers have bought 8kg of gummibears, 3kg of chocolate,
3kg of mint',
'In calendar week (3), customers have bought 9kg of gummibears, 5kg of chocolate',
'In calendar week (4), customers have bought 4kg of gummibears, 9kg of mint',
'In calendar week (5), customers have bought 1kg of chocolate, 2kg of mint',
'In calendar week (6), customers have bought nothing']

关于python - 来自 Pandas Dataframe 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52586780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com