sara
sara

Reputation: 59

what is the best way to add a column to a large data set on a csv file using python?

i have a data (3000 rows and 20000 columns) i need to add a column with a header of 'class' and the all 3000 rows contain the same word which is here 'Big' how i can do that using python? i tried to do it manually but the file is too large for excel it can't be loaded completely. I know it may seems easy but i'm new to python tried several codes but non of them gave the needed result.

Upvotes: 2

Views: 1045

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

Use Pandas module:

import pandas as pd

df = pd.read_csv(r'/path/to/file.csv').assign(Class='Big')
df.to_csv('/path/to/new_file.csv', index=False)

or as a one-liner:

pd.read_csv(r'/path/to/file.csv').assign(Class='Big') \ 
  .to_csv(r'/path/to/new_file.csv', index=False)

UPDATE:

I have 9 files as the one you just helped me to add a column to, each one represent a class's attributes. can you tell me how i can combine these files in one csv file, that will be 27000 rows and 30000 columns?

files = ['file1.csv','file2.csv', ...]

df = pd.concat([pd.read_csv(f) for f in files], ignore_index=True)

Upvotes: 3

Related Questions