第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將不同列大小的熊貓數(shù)據(jù)框拆分為單獨(dú)的數(shù)據(jù)框?

如何將不同列大小的熊貓數(shù)據(jù)框拆分為單獨(dú)的數(shù)據(jù)框?

慕慕森 2022-06-22 16:24:26
我有一個(gè)大熊貓數(shù)據(jù)框,由整個(gè)數(shù)據(jù)框中不同數(shù)量的列組成。這是一個(gè)示例:當(dāng)前數(shù)據(jù)框示例我想根據(jù)它擁有的列數(shù)將數(shù)據(jù)框拆分為多個(gè)數(shù)據(jù)框。此處的示例輸出圖像:輸出圖像謝謝。
查看完整描述

2 回答

?
滄海一幻覺

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果我說得對(duì),您要做的是將現(xiàn)有的 1 個(gè)數(shù)據(jù)框與n列拆分為ceil(n/5)數(shù)據(jù)框,每個(gè)數(shù)據(jù)框有 5 列,最后一個(gè)帶有n/5.


如果是這種情況,這將起到作用:


import pandas as pd

import math


max_cols=5


dt={"a": [1,2,3], "b": [6,5,3], "c": [8,4,2], "d": [8,4,0], "e": [1,9,5], "f": [9,7,9]}


df=pd.DataFrame(data=dt)


dfs=[df[df.columns[max_cols*i:max_cols*i+max_cols]] for i in range(math.ceil(len(df.columns)/max_cols))]


for el in dfs:

    print(el)

并輸出:


   a  b  c  d  e

0  1  6  8  8  1                                            

1  2  5  4  4  9                                            

2  3  3  2  0  5                                               

   f                                                        

0  9                                                        

1  7                                                        

2  9                                                        


[Program finished]


查看完整回答
反對(duì) 回復(fù) 2022-06-22
?
波斯汪

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果您有一個(gè)說 10 列的數(shù)據(jù)框,并且您想將具有 3 個(gè)NaN值的記錄與具有 1 的結(jié)果數(shù)據(jù)框一樣放在另一個(gè)結(jié)果數(shù)據(jù)框中NaN,您可以按如下方式執(zhí)行此操作:


# evaluate the number of NaNs per row

num_counts=df.isna().sum('columns')

# group by this number and add the grouped

# dataframe to a dictionary

results= dict()

num_counts=df.isna().sum('columns')

for key, sub_df in df.groupby(num_counts):

    results[key]= sub_df

執(zhí)行此代碼后,結(jié)果包含子集,df其中每個(gè)子集包含相同數(shù)量的NaNs(因此相同數(shù)量的非NaNs)。


如果要將結(jié)果寫入 excel 文件,只需執(zhí)行以下代碼:


with pd.ExcelWriter('sorted_output.xlsx') as writer:

    for key, sub_df in results.items():

        # if you want to avoid the detour of using dicitonaries

        # just replace the previous line by

        # for key, sub_df in df.groupby(num_counts):

        sub_df.to_excel(

            writer,

            sheet_name=f'missing {key}',

            na_rep='',

            inf_rep='inf',

            float_format=None,

            index=True,

            index_label=True,

            header=True)

例子:


# create an example dataframe

df=pd.DataFrame(dict(a=[1, 2, 3, 4, 5, 6], b=list('abbcac')))

df.loc[[2, 4, 5], 'c']= list('xyz')

df.loc[[2, 3, 4], 'd']= list('vxw')

df.loc[[1, 2], 'e']= list('qw')

它看起來像這樣:


Out[58]: 

   a  b    c    d    e

0  1  a  NaN  NaN  NaN

1  2  b  NaN  NaN    q

2  3  b    x    v    w

3  4  c  NaN    x  NaN

4  5  a    y    w  NaN

5  6  c    z  NaN  NaN

如果你在這個(gè)數(shù)據(jù)幀上執(zhí)行上面的代碼,你會(huì)得到一個(gè)包含以下內(nèi)容的字典:


0:    a  b  c  d  e

   2  3  b  x  v  w


1:    a  b  c  d    e

   4  5  a  y  w  NaN


2:    a  b    c    d    e

   1  2  b  NaN  NaN    q

   3  4  c  NaN    x  NaN

   5  6  c    z  NaN  NaN


3:    a  b    c    d    e

   0  1  a  NaN  NaN  NaN

字典的鍵是行中 s 的數(shù)量,NaN值是數(shù)據(jù)幀,其中僅包含具有該數(shù)量NaNs 的行。


查看完整回答
反對(duì) 回復(fù) 2022-06-22
  • 2 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)