[英]How can I split a csv file into two files based on values in a column in Python?
I have a csv file with 22 columns and X number of rows.我有一个 csv 文件,它有 22 列和 X 行。 How can I split the csv file into 2 separate files depending on what value is in the eg "item_number" column?如何根据“item_number”列中的值将 csv 文件拆分为 2 个单独的文件?
So for example, if a row has "1234" or "2345" in the "item_number" column, I want it (the whole row with all its respective columns included) to go into csv_file_1.因此,例如,如果一行在“item_number”列中有“1234”或“2345”,我希望它(包括所有相应列的整行)到 go 到 csv_file_1。 If a row has "5678" or "6789" in the "item_number" column, I want it to go into csv_file_2.如果一行在“item_number”列中有“5678”或“6789”,我希望它到 go 到 csv_file_2。
I want the code to loop over all of the X number of rows in the original input csv file.我希望代码循环遍历原始输入 csv 文件中的所有 X 行。
So, the output should be two separate csv files sorted depending on specific "item_number" values.因此,output 应该是两个独立的 csv 文件,根据特定的“item_number”值排序。
Thank you in advance for your help, and please let me know if more details are needed!提前感谢您的帮助,如果需要更多详细信息,请告诉我!
Is this what you are looking for?这是你想要的?
import pandas
data = pandas.read_csv('mycsv.csv')
# just make sure to remove the quotation if the numbers are not string
csv2 = data[data['item_number'].isin(['5678','6789'])]
csv1 = data[data['item_number'].isin(['1234','2345']]
csv1.to_csv('csv1.csv', index=False)
csv2.to_csv('csv2.csv', index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.