【问题标题】:Text from Pandas Dataframe来自 Pandas 数据框的文本
【发布时间】:2018-10-01 16:21:25
【问题描述】:

我有一个 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.'

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

>>> "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."

【问题讨论】:

  • gb_count = '' if pd['gummibears'] > 0: gb_count = '客户已购买' + pd['gummibears'].map(str) + 'kg of gummibears,'跨度>

标签: python pandas loops filtering


【解决方案1】:

您可以将自定义函数与numpy.whereeq (==) 创建的布尔掩码一起使用,但一般解决方案必须对文本进行规范化:

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']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2018-05-04
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多