python-3.x - 将 CSV 文件拆分为两个文件,在两个文件中保留标题
问题描述
我正在尝试将一个大型 CSV 文件拆分为两个文件。我正在使用下面的代码
import pandas as pd
#csv file name to be read in
in_csv = 'Master_file.csv'
#get the number of lines of the csv file to be read
number_lines = sum(1 for row in (open(in_csv)))
#size of rows of data to write to the csv,
#you can change the row size according to your need
rowsize = 600000
#start looping through data writing it to a new file for each set
for i in range(0,number_lines,rowsize):
df = pd.read_csv(in_csv,
nrows = rowsize,#number of rows to read at each loop
skiprows = i)#skip rows that have been read
#csv to write data to a new file with indexed name. input_1.csv etc.
out_csv = 'File_Number' + str(i) + '.csv'
df.to_csv(out_csv,
index=False,
header=True,
mode='a',#append data to csv file
chunksize=rowsize)#size of data to append for each loop
它正在拆分文件,但在第二个文件中缺少标题。我该如何解决
解决方案
.read_csv()
使用时返回一个迭代器chunksize
,然后跟踪标头。下面是一个例子。这应该快得多,因为上面的原始代码读取整个文件以计算行数,然后在每次块迭代中重新读取所有先前的行;而下面只读取一次文件:
import pandas as pd
with pd.read_csv('Master_file.csv', chunksize=60000) as reader:
for i,chunk in enumerate(reader):
chunk.to_csv(f'File_Number{i}.csv', index=False, header=True)
推荐阅读
- python - 将测地线数据类型更改为整数
- sequelize.js - 如何使用 findone 从 Sequelize 模型中获取所需的列数据
- python - 同时使用 scedule 和 Telebot
- reactjs - React 中的 Pdf 有时不显示
- nginx - *Nginx* ERR_TOO_MANY_REDIRECTS 重定向循环
- typescript - 如何将接口键入为 Dispatch
? - c - 如何在 Linux 上使用多个加密狗?
- database - MongoDB 4.4:$filter 的输入必须是一个不长的数组
- python - 从数据库导入数据时缺少列名(python + postgre sql)
- android-studio - 灰色资源 - xmlns:tools="http://schemas.android.com/tools" 和 xmlns:card_view="http://schemas.android.com/apk/res-auto"