vesuvius
vesuvius

Reputation: 435

How to add multiple new columns to existing csv file without mentioning the column name in python?

I want to add multiple columns in an existing csv. My data looks like this:

50451  51151  53266
 100    100    100
  1      1      1

where the data starting with (50...) are the columns and below them are the rows. I have another dataset which looks similar to this:

50014  50013  54567
 50     100    100

I am using this code to change it into csv:

df.to_csv('fort.csv', index = False)

but what it does is , it replaces the old columns with new ones. Since , I have to add multiple columns , I can't use df['50014'] everytime. If you guys could suggest something , I would greatly appreciate it.

Upvotes: 0

Views: 426

Answers (1)

prosti
prosti

Reputation: 46341

You can use merge suffixes, to achieve this. In case there are columns with the same names the suffixes will fix the problem.

suffixes : tuple of (str, str), default (‘_x’, ‘_y’)

Suffix to apply to overlapping column names in the left and right side, respectively. To raise an exception on overlapping columns use (False, False).


r = pd.merge(df, df, right_index=True, left_index=True, suffixes=('_left', '_right'), how='outer')
print(r)

For simplicity, I took the same df again:

   50451  51151  53266
0    100    100    100
1      1      1      1
   50451_left  51151_left  53266_left  50451_right  51151_right  53266_right
0         100         100         100          100          100          100
1           1           1           1            1            1            1

Important is to use the outer join.


Merge is same as join except join may be handy because it is by default used on indices (indexes). They share the same code base ;).

Upvotes: 3

Related Questions