![](/img/trans.png)
[英]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.