How To Split One Csv Into Multiple Files In Python
I have a csv file (world.csv) looks like this : 'city','city_alt','lat','lng','country' 'Mjekić','42.6781','20.9728','Kosovo' 'Mjekiff','42.6781','20.9728','Kosovo' 'paris','42.67
Solution 1:
If you can't use pandas you can use the built-in csv
module and itertools.groupby()
function. You can use this to group by country.
from itertools import groupby
import csv
withopen('world.csv') as csv_file:
reader = csv.reader(csv_file)
next(reader) #skip header#Group by column (country)
lst = sorted(reader, key=lambda x : x[4])
groups = groupby(lst, key=lambda x : x[4])
#Write file for each countryfor k,g in groups:
filename = k + '.csv'withopen(filename, 'w', newline='') as fout:
csv_output = csv.writer(fout)
csv_output.writerow(["city","city_alt","lat","lng","country"]) #headerfor line in g:
csv_output.writerow(line)
Solution 2:
try this:
filter the columns based on the country name. Then convert that to csv file using to_csv
in pandas
df = pd.read_csv('test.csv')
france = df[df['country']=='France']
kosovo = df[df['country']=='Kosovo']
morocco = df[df['country']=='Morocco']
france.to_csv('france.csv', index=False)
kosovo.to_csv('kosovo.csv', index=False)
morocco.to_csv('morocco.csv', index=False)
Solution 3:
The easiest way to do this is as below: #create a folder called "adata" for example in your working directory #import glob
for i,g in df.groupby('CITY'):
g.to_csv('adata\{}.csv'.format(i), header=True, index_label='Index')
print(glob.glob('adata\*.csv'))
filenames = sorted(glob.glob('adata\*.csv'))
for f in filenames:
#your intended processes
Post a Comment for "How To Split One Csv Into Multiple Files In Python"