'How to concat a list of dataframes depending on conditions
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!
Solution 1:[1]
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)
Solution 2:[2]
Try this :
df_new = pd.concat([dfs[1], dfs[3]])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Scott Boston |
| Solution 2 | DataSciRookie |
