pandas中DataFrame.to_dict()的實(shí)現(xiàn)示例
一、DataFrame.to_dict()
這是 pandas 庫(kù)中的一個(gè)方法,用于將 DataFrame 對(duì)象轉(zhuǎn)換為字典。這個(gè)方法非常有用,特別是在需要將 DataFrame 的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為 JSON 格式或其他與字典兼容的格式時(shí)。
參數(shù):
to_dict() 方法有幾個(gè)參數(shù)可選,用于控制輸出的格式。
- orient:指定字典格式。默認(rèn)為 'dict' ,表示每一列一個(gè)鍵。
- to_dict('records'):返回一個(gè)字典列表,每個(gè)字典代表一行記錄,鍵是列名,值是數(shù)據(jù)。
- to_dict('index'):返回一個(gè)字典,其中索引作為鍵,列名作為子鍵。
- to_dict('series'):類(lèi)似 'records',但返回的是一個(gè)列表,其中每個(gè)元素是一個(gè)字典。
- to_dict('split'):返回一個(gè)字典,包含兩個(gè)鍵:'index' 和 'columns',它們分別映射到索引和列名的列表,值是數(shù)據(jù)。
- to_dict('long'):將 DataFrame 轉(zhuǎn)換為長(zhǎng)格式字典。
二、舉例
創(chuàng)建一個(gè) DataFrame
import pandas as pd
df = pd.DataFrame({
'Column1': [1, 2],
'Column2': ['A', 'B']
})to_dict('records')
dic = df.to_dict('records')
print(dic)
# >>> dic[1]
print(dic[1])
# >>> dic[1]['Column1']
print(dic[1]['Column1'])
[{'Column1': 1, 'Column2': 'A'}, {'Column1': 2, 'Column2': 'B'}]
>>> dic[1]{'Column1': 2, 'Column2': 'B'}
>>> dic[1]['Column1']2
to_dict('list')
lis = df.to_dict('list')
print(list)
# >>> list['Column1']
print(list['Column1']){'Column1': [1, 2], 'Column2': ['A', 'B']}
>>> list['Column1'][1, 2]
to_dict('series')
ser= df.to_dict('series')
print(ser)
# >>> series['Column1']
print(ser['Column1'])
{'Column1': 0 1
1 2
Name: Column1, dtype: int64, 'Column2': 0 A
1 B
Name: Column2, dtype: object}
>>> series['Column1']:0 1
1 2
Name: Column1, dtype: int64
to_dict('index')
ind = df.to_dict('index')
print(ind)
# >>> index[1]
print(ind[1])
# >>> index[1]['Column1']
print(ind[1]['Column1']){0: {'Column1': 1, 'Column2': 'A'}, 1: {'Column1': 2, 'Column2': 'B'}}
>>> index[1]:{'Column1': 2, 'Column2': 'B'}
>>> index[1]['Column1']2
到此這篇關(guān)于pandas中DataFrame.to_dict()的文章就介紹到這了,更多相關(guān)pandas中DataFrame.to_dict()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Scrapy啟動(dòng)報(bào)錯(cuò)invalid syntax的解決
這篇文章主要介紹了Scrapy啟動(dòng)報(bào)錯(cuò)invalid syntax的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
LangChain簡(jiǎn)化ChatGPT工程復(fù)雜度使用詳解
這篇文章主要為大家介紹了LangChain簡(jiǎn)化ChatGPT工程復(fù)雜度使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Python類(lèi)中使用cursor.execute()時(shí)語(yǔ)法錯(cuò)誤的解決方法
在 Python 類(lèi)中使用 cursor.execute() 時(shí),出現(xiàn)語(yǔ)法錯(cuò)誤(如 SyntaxError 或 SQL 語(yǔ)法相關(guān)錯(cuò)誤)通常是因?yàn)?nbsp;SQL 語(yǔ)句格式不正確、占位符使用不當(dāng),或參數(shù)傳遞方式不符合預(yù)期,以下是解決此類(lèi)問(wèn)題的常見(jiàn)方法和建議,需要的朋友可以參考下2024-09-09
Pytorch 擴(kuò)展Tensor維度、壓縮Tensor維度的方法
這篇文章主要介紹了Pytorch 擴(kuò)展Tensor維度、壓縮Tensor維度的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Django接收自定義http header過(guò)程詳解
這篇文章主要介紹了Django接收自定義http header過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python?torch.fft.rfft()函數(shù)用法示例代碼
大家應(yīng)該都知道新舊版的torch中的傅里葉變換函數(shù)在定義和用法上有所不同,下面這篇文章主要給大家介紹了關(guān)于Python?torch.fft.rfft()函數(shù)用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

