![](/img/trans.png)
[英]How to delete multiple pandas (python) dataframes from memory to save RAM?
[英]How to delete the first 4 rows from multiple excel files using python pandas (Dataframes)
目前我正在編寫一個結合多個 Excel 電子表格的程序。
我想知道如何在組合它們之前從每個電子表格中刪除前 4 行。 下面是嘗試刪除前 4 行的特定語句,但我收到錯誤消息。
frames[0:] = [df.drop(df.index[[0,3]]) for df in frames[0:]]
下面是完整的程序
import tkinter as tk
from tkinter import filedialog
from pathlib import Path
import pandas as pd
root = tk.Tk()
root.withdraw()
files = filedialog.askopenfilenames()
print("--------------")
print(files)
ExcelFileNames = [Path(x).name for x in files]
print("--------------")
print(type(ExcelFileNames))
print("--------------")
print(ExcelFileNames)
print("--------------")
print (ExcelFileNames[0])
print("--------------")
print("Number of files is:", len(ExcelFileNames))
# read them in
excels = [pd.ExcelFile(name) for name in ExcelFileNames]
# turn them into dataframes
frames = [x.parse(x.sheet_names[0], header=None,index_col=None) for x in excels]
frames[0:] = [df.drop(df.index[[0,3]]) for df in frames[0:]]
# delete the first row for all frames except the first
# i.e. remove the header row -- assumes it's the first
frames[1:] = [df[1:] for df in frames[1:]]
# concatenate them..
combined = pd.concat(frames)
# write it out
combined.to_excel("DNcombined.xlsx", header=False, index=False)
國際大學聯盟,
您可以在參數中添加skiprows以在循環列表時跳過這些行。
# read them in
excels = [pd.ExcelFile(name) for name in ExcelFileNames]
# turn them into dataframes
frames = [x.parse(x.sheet_names[0], header=None,index_col=None, skiprows=4) for x in excels]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.