日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

pandas.DataFrame.from_dict直接從字典構建DataFrame的方法_python

作者:旺仔的算法coding筆記 ? 更新時間: 2022-08-10 編程語言

pandas函數中pandas.DataFrame.from_dict 直接從字典構建DataFrame 。

參數解析

DataFrame from_dict()方法用于將Dict轉換為DataFrame對象。 此方法接受以下參數。

  • data: dict or array like object to create DataFrame.data :字典或類似數組的對象來創建DataFrame。
  • orient: The orientation of the data. The allowed values are (‘columns’, ‘index’), default is the ‘columns’. ?orient :數據的方向。 允許值為(“列”,“索引”),默認值為“列”。 Specify orient='index' to create the DataFrame using dictionary keys as rows:。 當參數orient為index值時,會將字典的keys作為DataFrame的行。(默認是keys變為列)
  • 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 :當方向為“索引”時,用作DataFrame標簽的值的列表。 如果與列方向一起使用,則會引發ValueError 。

實例 ?

1)By default the keys of the dict become the DataFrame columns:

默認是將字典的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: 參數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

原文鏈接:https://blog.csdn.net/wangwangstone/article/details/121304120

欄目分類
最近更新