pandas把dataframe等份分割
时间: 2023-10-17 17:01:00 浏览: 135
Pandas中DataFrame的分组/分割/合并的实现
要将一个DataFrame分割成等份,可以使用以下代码:
```python
import math
import pandas as pd
from tqdm import tqdm
def cut_df(file_name, n):
df = pd.read_csv(file_name)
df_num = len(df)
every_epoch_num = math.floor((df_num/n))
for index in tqdm(range(n)):
file_name = f'./XXX_{index}.csv'
if index < n-1:
df_tem = df[every_epoch_num * index: every_epoch_num * (index + 1)]
else:
df_tem = df[every_epoch_num * index:]
df_tem.to_csv(file_name, index=False)
```
其中,`file_name`是要切割的文件名,如果是Excel文件,只需修改读取函数即可。`n`是要将DataFrame分割成的份数。这段代码将把DataFrame分割成n个文件,并保存在当前目录下。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [pandas把dataframe平均分割成多份](https://blog.csdn.net/weixin_42599499/article/details/117809308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [pandas - DataFrame 基础计算(求和、平均值、最大值、最小值、中位数、众数、方差、标准差、分位数)](https://blog.csdn.net/weixin_42115865/article/details/131586865)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文