Use Elements In A List For Dataframe Names
I have a list like this: network = ['facebook','organic',instagram'] And I create 3 dataframes: facebook_count, organic_count, instagram_count that each type of network. facebook
Solution 1:
The most efficient solution would be to create a dictionary storing the dataframes:
d = {i + '_count': df[df.isin([i + ' installs'])] for i in network}
And to get a dataframe, do:
print(d['facebook_count'])
Output will be the facebook_count
dataframe with expected values
Solution 2:
An easy fix is to use dictionary
count = {} # creating a new dictionaryfor i inrange(len(network)+1):
count[network[i]+'_count'] = df[df.isin([network[i]+' installs'])]
This way, you are using the name facebook_count
,organic_count
and instagram_count
as keys of the dictionary
i.e. count['facebook_count'] = df[df.isin('facebook installs'])]
Post a Comment for "Use Elements In A List For Dataframe Names"