[英]how to fill up data with given rows from another file in python
Supposed I have 100 txt files, and every file has 20000 records, I would like to let every file have 25000 records, how to fill up data with another file to get every file with 25000 records?假设我有 100 个 txt 文件,每个文件有 20000 条记录,我想让每个文件有 25000 条记录,如何用另一个文件填充数据以获得每个文件有 25000 条记录?
when they are all in one directory, use this:当它们都在一个目录中时,请使用以下命令:
import os
import pandas as pd
path = "path/to/directory/"
dfs = [] # list of dataframes
for file in os.listdir(path):
if file.endswith(".txt"):
# edit with you separator of choice
dfs.append(pd.read_csv(file, sep=" ")
# edit with your axis of choice
# ignore axis is important so you don't have multiple indices
full_df = pd.concat(dfs, axis=0, ignore_index=True)
l = len(full_df)
n_dfs = l // 25000 + 1 # new number of dfs
for i in range(ndfs):
if i < (n_dfs - 1):
new_df = full_df[i * 25000: (i+1) * 25000]
else:
new_df = full_df[i * 25000:]
new_df.to_csv("path/to/new_df/file.txt", header=None, index=None, sep=' ', mode='a')
this should do.这应该做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.