简体   繁体   中英

Trying to find a way to simplify my block of code

I understand that this is not the most concise block of code and looking for ways to simplify it

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()

The intention of this is to return a dataframe that shows how many times each 'title' occurred in a given year as a percentage.

This is the sample input输入

This is the sample output样本输出

So I simplified this code by running a for loop through a list of the years 2009-2018 and applying a function to divide each item in each list by the total count in each list and multiplying it by 100 and then using pd.DataFrame to create a dataframe and specifying the index names I would be using

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

The general form will be

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

Or, as a list comprehension:

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

That should be able to replace your 3 sets of 10 repetitive lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM