Trying to append single column dfs to once csv. Each column representing theold dfs. Do not know how to stop the dfs from stacking in a csv file. Csv
master_df = pd.DataFrame()
for file in os.listdir('TotalDailyMCUSDEachPool'):
if file.endswith('.csv'):
master_df = master_df.append(pd.read_csv(file))
master_df.to_csv('MasterFile.csv', index=False)
CodePudding user response:
You should use master_df.append(pd.read_csv(file), axis=1)
for your purpose. Check this for more info: https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html.
An example is as follows:
df1 = pd.DataFrame(
{
"A": ["A0", "A1", "A2", "A3"],
}
)
df2 = pd.DataFrame(
{
"B": ["B2", "B3", "B6", "B7"],
}
)
result = pd.concat([df1, df2], axis=1)
# A B
# 0 A0 B2
# 1 A1 B3
# 2 A2 B6
# 3 A3 B7
CodePudding user response:
If appends works fine "out of the box" only if column names of datasets are the same and you want to just append rows. In this example:
master_df = pd.DataFrame()
df = pd.DataFrame({'Numbers': [1010, 2020, 3030, 2020, 1515, 3030, 4545]})
df2 = pd.DataFrame({'Others': [1015, 2132, 3030, 2020, 1515, 3030, 4545]})
master_df = master_df.append(df)
master_df = master_df.append(df2)
master_df
You would get this output:
Numbers Others
0 1010.0 NaN
1 2020.0 NaN
2 3030.0 NaN
3 2020.0 NaN
4 1515.0 NaN
5 3030.0 NaN
6 4545.0 NaN
0 NaN 1015.0
1 NaN 2132.0
2 NaN 3030.0
3 NaN 2020.0
4 NaN 1515.0
5 NaN 3030.0
6 NaN 4545.0
To prevent it, use pd.concat()
with axis=1
master_df = pd.concat([df,df2], axis = 1)
master_df
Which should result in good dataset
Numbers Others
0 1010 1015
1 2020 2132
2 3030 3030
3 2020 2020
4 1515 1515
5 3030 3030
6 4545 4545