首页 > 解决方案 > Python script to skip specific column in CSV files

问题描述

I have a Python code which filters the data according to specific column and creates multiple CSV files.

Here is my main csv file:

Name,    City,      Email
john     cty_1      a@g.com
jack     cty_1      b@g.com
...
Ross     cty_2      c@g.com
Rachel   cty_2      d@g.com
...

My python logic currently creates separate csv for separate city. Existing python logic is:

from itertools import groupby
import csv

with open('filtered_final.csv') as csv_file:
    reader = csv.reader(csv_file)
    next(reader) #skip header
    
    #Group by column (city)
    lst = sorted(reader, key=lambda x : x[1])
    groups = groupby(lst, key=lambda x : x[1])

    #Write file for each city
    for k,g in groups:
        filename = k[21:] + '.csv'
        with open(filename, 'w', newline='') as fout:
            csv_output = csv.writer(fout)

            csv_output.writerow(["Name","City","Email"])  #header
            for line in g:
                csv_output.writerow(line)

Now, I want to remove the "City" Column on each new CSV files.

标签: pythonpython-3.xpandascsv

解决方案


Then try to import like:

df = pd.read_csv('filtered_final.csv', usecols=['Name','Email'])

推荐阅读