在pandas中dataframe怎麼轉化為字典?

時間 2021-07-11 20:48:07

1樓:python小工具

可以試試to_dict方法

,這個函式呼叫help認識下它

,通過控制引數輸入可以輸出不同格式

,我只是舉了最簡單的例子

pandas文件對入參的說明

Parameters

orient

:str

Determines

thetype

ofthe

values

ofthe

dictionary.-

'dict'

(default):

dict

like}-

'list'

:dict

like

-'series'

:dict

like

-'split'

:dict

like

-'records'

:list

like

[,...,]

-'index'

:dict

like

}Abbreviations

areallowed.`

s`indicates

`series

`and`sp

`indicates

`split`.

>>>import

pandas

aspd

>>>df=

pd.read_excel(r

'D:/myExcel/1.xlsx'

)>>>dfA

BC0bob

1245

1millor

1523

2bob

3488

3bob

9823

>>>df.

to_dict(),

'B':

,'C':}

>>>

使用python中的pandas模組時,怎樣只對其中的浮點數求和,而忽略掉其中的字串呢?

黃寶臣 df a lambdax 0iftype x isstrelsex sum 先用if語句判斷是否是str型別,是的話轉成0就行了,最後運算。 本圖希 假設你的資料集,是某一列中存在混合型別的資料,那麼我們要做的就是把能夠進行浮點轉換的數挑出來的過程。將乙個 對應位置為True or Fals...

Pandas中ix和iloc有什麼區別?

泰初君 Is ix always better than loc and iloc since it is faster and supports integer and label access? SunnyCareer loc可以使用文字標籤,如loc name iloc使用序號,如iloc 1...

在使用pandas時,用to csv將50萬行資料寫入csv檔中效率很低,要50s,有更高效的方法嗎?

星空流 pandas 儲存資料除了to csv方法,還有其他方法,比如topickle 方法,可以儲存原始的資料型別,相應的使用read pickle方法進行讀取資料,速度非常快。儲存資料前可以先把資料型別轉換成category型別,這樣會節省很多空間 硬碟空間及執行記憶體 寫入時間也會縮短,而且下...