Search code examples
pandascsvconcatenation

Concatenating csv files side by side in pandas


I have 6 csv files like this:

A,5601093669
C,714840722
D,3311821086
E,3714631762
F,2359322409
G,4449445373
H,1321142307
I,3403144346
K,2941319082
L,5982421765
M,1431041943
N,2289666237
P,2944809622
Q,2266749163
R,3503618053
S,3995185703
T,3348978524
V,4184646229
W,790817778
Y,1747887712

And I would like to concatenate them side by side, ie:

A,5601093669,5601093669,5601093669,5601093669... C,714840722,714840722,714840722,714840722 ... D,3311821086,3311821086,3311821086,3311821086...

Or even make a data frame directly like this:

    Letters Counts1 Counts2 Counts3 ...
0   A   949038913 949038913 949038913 ...
1   C   154135114 154135114 154135114 ...
.
.
.

I tried to use pandas, but I only concatenate them one over the other like this:

    Letters Counts
0   A   949038913
1   C   154135114
2   D   602309784
3   E   672070230
4   F   430604264
5   G   760092523
6   H   242152981
7   I   608218717
8   K   558412515
9   L   1057894498
10  N   455966669
11  M   238551663
12  P   554657856
13  Q   423767129
14  R   650581191
15  S   819127381
16  T   632469374
17  V   717790671
18  W   144439568
19  Y   324996779
20  A   5601093669
21  C   714840722
22  D   3311821086
23  E   3714631762
24  F   2359322409
25  G   4449445373
26  H   1321142307
27  I   3403144346
28  K   2941319082
29  L   5982421765
30  M   1431041943
31  N   2289666237

the code was like this:

extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]


df_from_each_file = [pd.read_csv(f, header=None, names=['Latters', 'Counts']) 
                     for f in all_filenames]

frame = pd.concat(df_from_each_file, axis=0, ignore_index=True)

Any tip or improvement would be very welcome!

Thank you by your time!

Paulo


Solution

  • use axis=1 in pd.concat function you can refer https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html