0

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      [email protected]
jack     cty_1      [email protected]
...
Ross     cty_2      [email protected]
Rachel   cty_2      [email protected]
...

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.

2 Answers 2

1

If you data is small enough to put on ram, you can just read the whole thing in and do a groupby:

import pandas as pd

df = pd.read_csv('filtered_final.csv')

for city, data in df[['Name','Email']].groupby(df['City']):
    data.to_csv(f'{city}_data.csv', index=False)
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Quang, Here you have used 'pd'. Is it a library? Do we need to import that one?
@DeependraDangal it's standard shorthand for pandas, which you tagged.
Okay @Quang . It worked. But in the new CSV files, one additional identifier is automatically added in the first column(before Name and Email column)
Oh, I forgot to pass index=False into to_csv. See updates.
1

Then try to import like:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.