Split Csv File Into Multiple Files By Column-data
I want to split a 'source.csv' file based on it's contents. But of course it's not just simple splitting, but I need to fullfill some 'rules'. The source and destination files hav
Solution 1:
Do you have the python pandas
module? It is a great module for data processing and will help you very much. Something like this can help you on the way:
import pandas
csv = pandas.read_csv('test.csv', sep=',', header=3, skipinitialspace=True)
# header=3 because your header is on the third line# skipinitialspace is set to True because your example data has spaces after commas
csv_apples = csv[csv['Fruit'] == 'Apple']
csv_bananas = csv[csv['Fruit'] == 'Banana']
csv_apples.to_csv('apples.csv', index=False, sep=',')
csv_bananas.to_csv('bananas.csv', index=False, sep=',')
This example does not write the first 3 lines of your original csv to the resulting csv. You can implement this reading the first three lines the csv separately and passing it in the to_csv
function with header=yourheader
Post a Comment for "Split Csv File Into Multiple Files By Column-data"