0

As the post's title, what I found on internet are mainly merging with header. Browsing around only gives me this only code that can return a result

import pandas as pd
import glob
interesting_files = glob.glob("*.csv")
df_list = []
for filename in sorted(interesting_files):
    df_list.append(pd.read_csv(filename))
full_df = pd.concat(df_list)

full_df.to_csv('output.csv')

` but the result is not good at all as in this picture: enter image description here

what I want is just like this enter image description here

EDIT I found this one actually works really good for my case.

import pandas as pd
import glob
import csv

interesting_files = glob.glob("*.csv")
df_list = []
for filename in sorted(interesting_files):
    df_list.append(pd.read_csv(filename, header=None))
full_df = pd.concat(df_list, ignore_index=True, axis=1)
        
full_df.to_csv('Route_data.csv') #,header = ['a','b',...], index=False) for csv output header index=False)

additional code to delete the old files that just merged makes this even more powerful

NatL
  • 3
  • 3

1 Answers1

0

You can use:

pd.concat([df1, df2], axis=1)

Example:

import pandas as pd
df1 = pd.DataFrame([*zip([1,2,3],[4,5,6])])
print(df1)
df2 = pd.DataFrame([*zip([7,8,9],[10,11,12])])
print(df2)
df_combined = pd.concat([df1, df2], axis=1)
print(df_combined)

Output:

df1>
   0  1
0  1  4
1  2  5
2  3  6

df2>
   0   1
0  7  10
1  8  11
2  9  12

df_combined>
   0  1  0   1
0  1  4  7  10
1  2  5  8  11
2  3  6  9  12

DataFrame Example:

import pandas as pd
df1 = pd.read_csv("/path/file1.csv", header=None)
print(df1)
df2 = pd.read_csv("/path/file2.csv", header=None)
print(df2)
df_combined = pd.concat([df1, df2], axis=1)
print(df_combined)
itsDV7
  • 854
  • 5
  • 12
  • Hi but how can I use this for my csv files? – NatL Jan 24 '21 at 06:18
  • I've edited my answer. – itsDV7 Jan 24 '21 at 06:36
  • In case I just want to make path to my csv default so that I can repeat the process many times, what should the ("path/file1.csv") looks like? because every time I got new data, file names will not be the same as before. Thx! – NatL Jan 24 '21 at 10:33