试图找到一种方法来简化我的代码块

时间:2019-01-16 22:22:04

标签: python pandas data-science

我知道这不是最简洁的代码块,并且正在寻找简化它的方法

nine = fb_posts2[fb_posts2['year']==2009].groupby('title').size()
ten = fb_posts2[fb_posts2['year']==2010].groupby('title').size()
eleven = fb_posts2[fb_posts2['year']==2011].groupby('title').size()
twelve = fb_posts2[fb_posts2['year']==2012].groupby('title').size()
thirteen = fb_posts2[fb_posts2['year']==2013].groupby('title').size()
fourteen = fb_posts2[fb_posts2['year']==2014].groupby('title').size()
fifteen = fb_posts2[fb_posts2['year']==2015].groupby('title').size()
sixteen = fb_posts2[fb_posts2['year']==2016].groupby('title').size()
seventeen = fb_posts2[fb_posts2['year']==2017].groupby('title').size()
eighteen = fb_posts2[fb_posts2['year']==2018].groupby('title').size()
a1 = lambda x: x/sum(nine)*100
a2 = lambda x: x/sum(ten)*100
a3 = lambda x: x/sum(eleven)*100
a4 = lambda x: x/sum(twelve)*100
a5 = lambda x: x/sum(thirteen)*100
a6 = lambda x: x/sum(fourteen)*100
a7 = lambda x: x/sum(fifteen)*100
a8 = lambda x: x/sum(sixteen)*100
a9 = lambda x: x/sum(seventeen)*100
a10 = lambda x: x/sum(eighteen)*100
nine = a1(nine)
ten = a2(ten)
eleven = a3(eleven) 
twelve = a4(twelve)
thirteen = a5(thirteen)
fourteen = a6(fourteen)
fifteen = a7(fifteen)
sixteen = a8(sixteen)
seventeen = a9(seventeen)
eighteen = a10(eighteen)
my_names = [2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
cols = ['link', 'post','shared','timeline','status']
ser = [nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen]
df = pd.concat(ser, axis=1, keys=my_names)
df[2009].fillna(0, inplace=True)
df[2011].fillna(0, inplace=True)
df[2012].fillna(0, inplace=True)
df = df.transpose()

这样做的目的是返回一个数据框,以百分比形式显示给定年份中每个“标题”发生的次数。

这是示例输入 Input

这是示例输出Sample Output

2 个答案:

答案 0 :(得分:0)

因此,我通过以下操作简化了此代码:在2009-2018年的列表中运行for循环,并应用函数将每个列表中的每个项目除以每个列表中的总数,然后将其乘以100,然后使用pd。 DataFrame创建一个数据框并指定我要使用的索引名称

a = [x/sum(x)*100 for x in [nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen]]
pd.DataFrame(a, index= my_names)

答案 1 :(得分:0)

一般形式为

ser = []
for year in my_names:
    ser.append(
        x/sum(fb_posts2[fb_posts2['year']==year].groupby('title').size()) * 100

或者,作为列表理解:

ser = [x/sum(fb_posts2[fb_posts2['year']==year].groupby('title').size()) * 100
    for year in my_names]

那应该能够代替您的3组10条重复行。