[英]How to average across excel rows for multiple rows using Openpyxl in Python?
[英]How to split one excel into multiple excel with common number of rows distribution across all the new excel using Python?
我有一個大數據的excel。 我想將這個 excel 拆分成多個 excel,行分布相等。
我當前的代碼正在部分工作,因為它正在分配所需的行數並創建多個 excel。 但與此同時,它通過考慮行數不斷創建更多的 excel。
在n_partitions 中,如果我輸入數字 5,那么它會在兩個 excel 中創建 5 行的 excel,然后它會繼續創建另外三個空白 excel。 我希望我的代碼在分發所有行后停止創建更多 excel。
下面是我的示例 excel,帶有預期結果和示例代碼。
我目前使用的代碼是。
import pandas as pd
df = pd.read_excel("C:/Zen/TestZenAmp.xlsx")
n_partitions = 5
for i in range(n_partitions):
sub_df = df.iloc[(i*n_partitions):((i+1)*n_partitions)]
sub_df.to_excel(f"C:/Zen/-{i}.xlsx", sheet_name="a")
另一種可能的解決方案:
g = df.groupby([df.index // k])
df['id'] = g.ngroup()
(g.apply(lambda x: x.drop('id', 1)
.to_excel(f"/tmp/x-{pd.unique(x.id)[0]}.xlsx", sheet_name="a")))
這正是我想要的,但在 Java 版本中:(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.