[英]Divide a large dataframe into smaller sub dataframes in order
有沒有辦法將非常大的數據幀分成等份的較小的5個子數據幀? 我不能使用火車測試拆分,因為它沒有按順序保存數據。 已經存在的解決方案拆分了一個大的 pandas dataframe 。 它不符合我的目的。 我試過了,它給出了下面提到的 output,這是不需要的 output。輸入是
new_dict1 = {'ABW':{'ABR':1,'BPR':1,'CBR':1,'DBR':0},'BCW':{'ABR':0,'BPR':0,'CBR':1,'DBR':0},
'CBW':{'ABR':1,'BPR':1,'CBR':0,'DBR':0},'MCW':{'ABR':1,'BPR':1,'CBR':0,'DBR':1},
'DBW':{'ABR':0,'BPR':0,'CBR':1,'DBR':0},'MNW':{'ABR':0,'BPR':0,'CBR':1,'DBR':0},
'RBW':{'ABR':0,'BPR':0,'CBR':1,'DBR':0},'EBW':{'ABR':0,'BPR':0,'CBR':1,'DBR':0},
'GBW':{'ABR':0,'BPR':0,'CBR':1,'DBR':0},'HBW':{'ABR':0,'BPR':0,'CBR':1,'DBR':0}}
import pandas as pd
df2 = pd.DataFrame.from_dict(new_dict1,orient="index")
我得到的 output 是
[ ABR BPR CBR DBR
ABW 1 1 1 0
BCW 0 0 1 0
CBW 1 1 0 0
MCW 1 1 0 1
DBW 0 0 1 0, ABR BPR CBR DBR
MNW 0 0 1 0
RBW 0 0 1 0
EBW 0 0 1 0
GBW 0 0 1 0
HBW 0 0 1 0]
這不是想要的 output。想要的 output 是將大的 dataframe 分成五個子數據場。
按照我的評論。 這是一個例子,注意它可能不是最好的方法..:
import numpy as np
dfs = np.array_split(df2, 5)
for index, df in enumerate(dfs):
globals()['df%s' % index] = pd.DataFrame(df)
df3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.