簡體   English   中英

如何用 python 中另一個文件的給定行填充數據

[英]how to fill up data with given rows from another file in python

假設我有 100 個 txt 文件,每個文件有 20000 條記錄,我想讓每個文件有 25000 條記錄,如何用另一個文件填充數據以獲得每個文件有 25000 條記錄?

當它們都在一個目錄中時,請使用以下命令:

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')

這應該做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM