我正在尝试获取最终/摘要数据框,并通过将文本与该数据框中各个系列的数字/数据连接起来来创建书面摘要。以下是我希望看到的脚本
Team Stat1 Stat2 Stat3 total increase/decrease written script
red 8 -6 2 4 increase the increase is driven by stat1 8, stat2 -6 and stat3 2
blue -4 2 1 -1 decrease the decrease is driven by stat1 -4, stat2 2 and stat3 1
green 2 10 3 15 increase the increase is driven by stat2 10, stat3 3 and stat1 2
增加/减少列基于查看总计列的if语句
每个团队的脚本需要按最大统计编号从绝对值从最大到最小的顺序进行排序。我可以将串联和数字放在一起,但是我不知道如何排序字符串列表或将值分配给不同列中的一系列字符串。任何指导将不胜感激。
答案 0 :(得分:0)
Lambda函数将有助于满足此要求。
def summaryText(x):
text = 'the {} is driven by {},{} and {}'.format(x['total'],\
'Stat1'+ " "+str(x['Stat1']),\
" "+'Stat2'+ " "+str(x['Stat2']),\
" "+'Stat3'+" "+ str(x['Stat3']))
return text
df['summary'] = df.apply(lambda x: summaryText(x),axis=1)
df['increase/decrease'] = df.apply(lambda x : 'increase' if x['total'] > 0 else 'decrease',axis=1)
尽管一个线性函数和调用函数在本质上都包含相同的内容,但我还是希望共享如下所示的%timeit magic屏幕截图,以进行总列检查。 ![正在加载图像] [![PercenttimeitMagic屏幕截图] 1] 1