简体   繁体   中英

Split 2 csv files into smaller sets of files based on unique values in python

Sorry if this question has been asked before, I just couldn't find a simple example.

I have 2 large CSV files that I would like to split based on the unique values in the Location & LocationType Column. I would like to store the split csv files into sub-directories for each value in a folder named item/{item_name} where item_name is the unique value in Location & Location_type.

  • Location.csv

CSV-1

  • Location-type.csv

CSV-2

  • Each split csv file should have the same header line as the parent file
  • If the sub-directory already exists, delete those files before writing the new files.

End result would be a directory called item with two sub-directories called fm5 & fm15 with our split CSV files stored. location.csv & location_type.csv

Thank you in advance

would like to know the workflow for this type of project

  • open the file
  • sort the contents on the desired column
  • group by the desired column
  • write each group to a new file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM