简体   繁体   English

如何根据 Python 中的列中的值将 csv 文件拆分为两个文件?

[英]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.

相关问题 如何在python中拆分csv文件? - How can I split csv files in python? 在Python中,如何根据一列中的值比较两个csv文件并从第一个文件中输出与第二个不匹配的记录 - In Python, how to compare two csv files based on values in one column and output records from first file that do not match second 如何为CSV文件中的每一列提取两个字符之间的子字符串,并将这些值复制到Python中的新列中? - How can I extract a substring between two characters for every row of a column in a CSV file and copy those values into a new column in Python? 如何基于通用列在Python中合并两个CSV文件 - How to Merge Two CSV Files in Python Based on Common Column 根据 python 中的唯一值将 2 个 csv 文件拆分为较小的文件集 - Split 2 csv files into smaller sets of files based on unique values in python 如何在python中从csv文件中拆分列 - How can I split columns from csv files in python 如何根据值将CSV文件拆分为2个单独的CSV文件 - How to split CSV file into 2x separate CSV files based on values 如何使用 Python 根据 CSV 文件中的列中的条件插入值? - How to insert values based on condition in column in the CSV file using Python? 根据 csv 文件值重命名文件 - Python - Rename files based on csv file values - Python 使用R或Python根据多行值将csv拆分为较小的文件 - Split csv into smaller files based on multiple rows values with R or Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM