pandas.DataFrame.from_dict直接從字典構(gòu)建DataFrame的方法
pandas函數(shù)中pandas.DataFrame.from_dict 直接從字典構(gòu)建DataFrame 。
參數(shù)解析
DataFrame from_dict()方法用于將Dict轉(zhuǎn)換為DataFrame對象。 此方法接受以下參數(shù)。
- data: dict or array like object to create DataFrame.data :字典或類似數(shù)組的對象來創(chuàng)建DataFrame。
- orient: The orientation of the data. The allowed values are (‘columns’, ‘index’), default is the ‘columns’. orient :數(shù)據(jù)的方向。 允許值為(“列”,“索引”),默認(rèn)值為“列”。 Specify orient='index' to create the DataFrame using dictionary keys as rows:。 當(dāng)參數(shù)orient為index值時,會將字典的keys作為DataFrame的行。(默認(rèn)是keys變?yōu)榱校?/li>
- columns: a list of values to use as labels for the DataFrame when orientation is ‘index’. If it’s used with columns orientation, ValueError is raised. columns :當(dāng)方向為“索引”時,用作DataFrame標(biāo)簽的值的列表。 如果與列方向一起使用,則會引發(fā)ValueError 。
實例
1)By default the keys of the dict become the DataFrame columns:
默認(rèn)是將字典的keys作為列
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']} pd.DataFrame.from_dict(data) col_1 col_2 0 3 a 1 2 b 2 1 c 3 0 d
2) Specify orient='index' to create the DataFrame using dictionary keys as rows: 參數(shù)orient為index值時,會將字典的keys作為DataFrame的行
data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']} pd.DataFrame.from_dict(data, orient='index') 0 1 2 3 row_1 3 2 1 0 row_2 a b c d
3) orient為index值時, 可以手動命名列名
pd.DataFrame.from_dict(data, orient='index', columns=['A', 'B', 'C', 'D']) A B C D row_1 3 2 1 0 row_2 a b c d
參考: pandas.DataFrame.from_dict — pandas 1.3.4 documentation
到此這篇關(guān)于pandas.DataFrame.from_dict直接從字典構(gòu)建DataFrame的方法的文章就介紹到這了,更多相關(guān)pandas字典構(gòu)建DataFrame內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python入門for循環(huán)嵌套理解學(xué)習(xí)
這篇文章主要介紹了python入門關(guān)于for循環(huán)嵌套的理解學(xué)習(xí),希望大家可以學(xué)會并運用到日常工作中,有需要的朋友可以借鑒參考下,希望能夠有幫助2021-09-09Python+tkinter制作經(jīng)典登錄界面和點擊事件
Tkinter是?Python?標(biāo)準(zhǔn)?GUI?庫,簡稱?“Tk”;從本質(zhì)上來說,它是對?TCL/TK?工具包的一種?Python?接口封裝。本文將利用tkinter制作一個經(jīng)典的登錄界面和點擊事件,需要的可以參考一下2022-09-09python網(wǎng)絡(luò)爬蟲之如何偽裝逃過反爬蟲程序的方法
本篇文章主要介紹了python網(wǎng)絡(luò)爬蟲之如何偽裝逃過反爬蟲程序的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11