Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据框中的文本_Python_Pandas_Loops_Filtering - Fatal编程技术网

Python 数据框中的文本

Python 数据框中的文本,python,pandas,loops,filtering,Python,Pandas,Loops,Filtering,我有一个熊猫数据框,其中包含出售的树胶耳朵、巧克力和薄荷糖的单个事件。它们按周数进行汇总和排序。我现在将其翻译成文本,然后通过电子邮件发送,使用以下方法: pd['text'] = 'In calendar week (' + pd['weeknumber'].map(str) + '), customers have bought ' + pd['gummibears'].map(str) + 'kg of gummibears, ' + pd['chocolate'].map(str) +

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

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."
>>> "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."
这是可行的,但让读者感到困惑。有没有一种优雅的方法可以过滤掉所有带有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."
>>> "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."

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

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']
如果pd['gummibears']>0:gb_count='客户已购买'+pd['gummibears']],则gb_count=''。映射(str)+'千克gummibears,'