7

I have the following code in Python to delete the first row from csv files in the folder in_folder and then save them in the folder out_folder. Now I need to delete the first column of a csv file. Does anyone know how to do this?

import csv
import glob
import os
import shutil

path = 'in_folder/*.csv'
files=glob.glob(path)

#Read every file in the directory

x = 0 #counter

for filename in files:

    with open(filename, 'r') as fin:
        data = fin.read().splitlines(True)

        with open(filename, 'w') as fout:
            fout.writelines(data[1:])
            x+=1
            print(x)

dir_src = "in_folder"
dir_dst = "out_folder"


for file in os.listdir(dir_src):
    if x>0:
        src_file = os.path.join(dir_src, file)
        dst_file = os.path.join(dir_dst, file)
        shutil.move(src_file, dst_file)
2
  • theres lots of ways to do it. You import csv but don't use it? Commented Feb 8, 2019 at 16:38
  • Yes, now I noticed that I don't use it. Commented Feb 8, 2019 at 16:40

1 Answer 1

20

What you can do is to use Pandas as it can achive DataFrame manipulation.

file.csv

1,2,3,4,5
1,2,3,4,5
1,2,3,4,5

Your code should look like

import pandas as pd
df = pd.read_csv('file.csv')
# If you know the name of the column skip this
first_column = df.columns[0]
# Delete first
df = df.drop([first_column], axis=1)
df.to_csv('file.csv', index=False)

file.csv

2,3,4,5
2,3,4,5
2,3,4,5
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this, it saves a new file, but the first column is not deleted.
add index=False to the to_csv function, and be sure to assign the dropped dataframe to a variable.
Now it works, thanks!
it's pd.read_csv not read_csv

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.