user12321371
user12321371

Reputation:

Sort out columns of multiple csv files at once in Python

really appreciate your help.

I have around 200 csv files with same header. eg of headers are x , y, z, time, id, type

I would like to sort out time colums of all csv files and save them again.

This is so far I have tried. But it doesn't work.

Could you please help me ??

Thank you


import csv
import operator
import glob
import pandas as pd

data = dict() # filename : lists

path="./*.csv"
files=glob.glob(path)

for filename in files:
    # process each file
    with open(filename, 'r') as f:
        # read file to a list of lists
        lists = [row for row in csv.reader(f, delimiter=',')]
        # sort and save into a dict
        sorted_df = lists.sort_values(by=["time"], ascending=True)
        sorted_df.to_csv('%.csv', index=False)

Upvotes: 1

Views: 259

Answers (1)

popeye
popeye

Reputation: 896

I don't have much knowledge about the csv module but you're using pandas and it supports reading csv files with pd.read_csv, why not utilize that..

for filename in files:
  df = pd.read_csv(filename)
  df.sort_values('time', inplace=True)
  df.to_csv(filename, index=False)

This would overwrite all the files with same data sorted by time.

Upvotes: 1

Related Questions