I have a list of dataframes:
list_df = [df1, df2, df3, df4]
And each dataframe looks like this:
df1=pd.DataFrame([[2020-02,2020-01],['PC','PC'],[0.6,1.4],[0.5,1.3]], columns=['Date', 'platform', "Day 1","Day 7"])
df2=pd.DataFrame([[2020-02,2020-01],['Mobile','Mobile'],[0.6,1.4],[0.5,1.3]], columns=['Date', 'platform', "Day 1","Day 7"])
df3=pd.DataFrame([[2020-03,2020-04],['PC','PC'],[0.6,1.4],[0.5,1.3]], columns=['Date', 'platform', "Day 1","Day 7"])
df4=pd.DataFrame([[2020-03,2020-04],['Mobile','Mobile'],[0.6,1.4],[0.5,1.3]], columns=['Date', 'platform', "Day 1","Day 7"])
I want to concat those dataframes inside the list that have the same platform value. In this case, I would need to concat df1 df3 and df2 df4.
I have tried this:
df_new = pd.concat(dfs[1], dfs[3])
But it returns the following error message:
first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"
Do you know any way to concat those dataframes with the same platform name from the list without creating only one dataframe and then filter it?
Thanks!
CodePudding user response:
I think you need:
pd.concat([list_df[0], list_df[2]])
pd.concat([list_df[1], list_df[3]])
platform_list = []
for _, g in pd.concat(list_df).groupby(['platform']):
platform_list.append(g)
CodePudding user response:
Try this :
df_new = pd.concat([dfs[1], dfs[3]])
CodePudding user response:
Você precisa armazenar os dataframes em forma de lista em uma variavel assim como você fez com seu "list_df
"e ai sim acionar a função 'concat' para concatenar todos os "dataframes" dentro da variavel com a lista dos mesmos
try this solution:
list_df = pd.concat(list_df)
print(list_df)