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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

pandas中字典和dataFrame的相互轉(zhuǎn)換_python

作者:淡定的炮仗 ? 更新時(shí)間: 2022-11-07 編程語(yǔ)言

一、字典轉(zhuǎn)dataFrame

1、字典轉(zhuǎn)dataFrame比較簡(jiǎn)單,直接給出示例:

import pandas as pd
dic = {
'name':['張三','李四','王二','麻子','小紅','小蘭','小玉','小強(qiáng)','小娟','小明'],
'num':[802,807,801,803,806,805,808,809,800,804],
'height': [183, 161, 163, 163, 156, 186, 184, 154, 153, 174],
'weight': [87, 60, 71, 74, 45, 50, 47, 67, 49, 70],
'gender': ['男', '男', '男', '男', '女', '女', '女', '男', '女', '男'],
'age': [25, 30, 25, 26, 27, 20, 23, 26, 30, 30]
}
df=pd.DataFrame(dic)
print(df)

結(jié)果:

二、dataFrame轉(zhuǎn)字典

1、DataFrame.to_dict() 函數(shù)介紹

pandas中經(jīng)常用的是 DataFrame.to_dict() 函數(shù)將dataFrame轉(zhuǎn)化為字典類(lèi)型(字典的查詢(xún)速度很快

函數(shù)DataFrame.to_dict(orient=‘dict’, into=<class ‘dict’>)

  • orient =‘dict’,是函數(shù)默認(rèn)的,轉(zhuǎn)化后的字典形式:{column(列名) : {index(行名) : value(值)}};
  • orient =‘list’ ,轉(zhuǎn)化后的字典形式:{column(列名) :{[values](值)}};
  • orient =‘series’ ,轉(zhuǎn)化后的字典形式:{column(列名) : Series (values) (值)};
  • orient =‘split’ ,轉(zhuǎn)化后的字典形式:{‘index’ : [index],‘columns’ :[columns],’data‘ : [values]};
  • orient =‘records’ ,轉(zhuǎn)化后是 list形式:[{column(列名) :value(值)}…{column:value}];
  • orient =‘index’ ,轉(zhuǎn)化后的字典形式:{index(值) :{column(列名) : value(值)}};

dataFrame.to_dict() 結(jié)果默認(rèn) index 是 key ,其他字段是和 index 對(duì)應(yīng)的 value

2、orient =‘dict’

orient =‘dict’ 是函數(shù)默認(rèn)的,轉(zhuǎn)化后的字典形式:{column(列名) : {index(行名) : value(值)}}

dic1 = df.to_dict()
print(dic1)

結(jié)果:

{
'name': {0: '張三', 1: '李四', 2: '王二', 3: '麻子', 4: '小紅', 5: '小蘭', 6: '小玉', 7: '小強(qiáng)', 8: '小娟', 9: '小明'},?
'num': {0: 802, 1: 807, 2: 801, 3: 803, 4: 806, 5: 805, 6: 808, 7: 809, 8: 800, 9: 804},?
'height': {0: 183, 1: 161, 2: 163, 3: 163, 4: 156, 5: 186, 6: 184, 7: 154, 8: 153, 9: 174},?
'weight': {0: 87, 1: 60, 2: 71, 3: 74, 4: 45, 5: 50, 6: 47, 7: 67, 8: 49, 9: 70},?
'gender': {0: '男', 1: '男', 2: '男', 3: '男', 4: '女', 5: '女', 6: '女', 7: '男', 8: '女', 9: '男'},?
'age': {0: 25, 1: 30, 2: 25, 3: 26, 4: 27, 5: 20, 6: 23, 7: 26, 8: 30, 9: 30}
}

3、 orient =‘list’

orient =‘list’ ,轉(zhuǎn)化后的字典形式:{column(列名) :{[values](值)}};

dic1 = df.to_dict('list')
print(dic1)

結(jié)果:

{
'name': ['張三', '李四', '王二', '麻子', '小紅', '小蘭', '小玉', '小強(qiáng)', '小娟', '小明'],?
'num': [802, 807, 801, 803, 806, 805, 808, 809, 800, 804],?
'height': [183, 161, 163, 163, 156, 186, 184, 154, 153, 174],?
'weight': [87, 60, 71, 74, 45, 50, 47, 67, 49, 70],?
'gender': ['男', '男', '男', '男', '女', '女', '女', '男', '女', '男'],?
'age': [25, 30, 25, 26, 27, 20, 23, 26, 30, 30]
}

4、orient =‘series’

orient =‘series’ ,轉(zhuǎn)化后的字典形式:{column(列名) : Series (values) (值)}

dic1 = df.to_dict('series')
print(dic1)

結(jié)果:

{
'name':?
0 ? ?張三
1 ? ?李四
2 ? ?王二
3 ? ?麻子
4 ? ?小紅
5 ? ?小蘭
6 ? ?小玉
7 ? ?小強(qiáng)
8 ? ?小娟
9 ? ?小明
Name: name, dtype: object,?
'num':?
0 ? ?802
1 ? ?807
2 ? ?801
3 ? ?803
4 ? ?806
5 ? ?805
6 ? ?808
7 ? ?809
8 ? ?800
9 ? ?804
Name: num, dtype: int64,?
'height':
0 ? ?183
1 ? ?161
2 ? ?163
3 ? ?163
4 ? ?156
5 ? ?186
6 ? ?184
7 ? ?154
8 ? ?153
9 ? ?174
Name: height, dtype: int64,?
'weight':?
0 ? ?87
1 ? ?60
2 ? ?71
3 ? ?74
4 ? ?45
5 ? ?50
6 ? ?47
7 ? ?67
8 ? ?49
9 ? ?70
Name: weight, dtype: int64,?
'gender':?
0 ? ?男
1 ? ?男
2 ? ?男
3 ? ?男
4 ? ?女
5 ? ?女
6 ? ?女
7 ? ?男
8 ? ?女
9 ? ?男
Name: gender, dtype: object,?
'age':?
0 ? ?25
1 ? ?30
2 ? ?25
3 ? ?26
4 ? ?27
5 ? ?20
6 ? ?23
7 ? ?26
8 ? ?30
9 ? ?30
Name: age, dtype: int64}

5、orient =‘split’

orient =‘split’ ,轉(zhuǎn)化后的字典形式:{‘index’ : [index],‘columns’ :[columns],’data‘ : [values]}

{'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],?
'columns': ['name', 'num', 'height', 'weight', 'gender', 'age'],?
'data': [
['張三', 802, 183, 87, '男', 25],
['李四', 807, 161, 60, '男', 30],?
['王二', 801, 163, 71, '男', 25],?
['麻子', 803, 163, 74, '男', 26],?
['小紅', 806, 156, 45, '女', 27],?
['小蘭', 805, 186, 50, '女', 20],?
['小玉', 808, 184, 47, '女', 23],?
['小強(qiáng)', 809, 154, 67, '男', 26],?
['小娟', 800, 153, 49, '女', 30],?
['小明', 804, 174, 70, '男', 30]
]
}

6、orient =‘records’

orient =‘records’ ,轉(zhuǎn)化后是 list形式:[{column(列名) :value(值)}…{column:value}]

dic1 = df.to_dict('records')
print(dic1)

結(jié)果:

[
{'name': '張三', 'num': 802, 'height': 183, 'weight': 87, 'gender': '男', 'age': 25},?
{'name': '李四', 'num': 807, 'height': 161, 'weight': 60, 'gender': '男', 'age': 30},?
{'name': '王二', 'num': 801, 'height': 163, 'weight': 71, 'gender': '男', 'age': 25},?
{'name': '麻子', 'num': 803, 'height': 163, 'weight': 74, 'gender': '男', 'age': 26},?
{'name': '小紅', 'num': 806, 'height': 156, 'weight': 45, 'gender': '女', 'age': 27},?
{'name': '小蘭', 'num': 805, 'height': 186, 'weight': 50, 'gender': '女', 'age': 20},?
{'name': '小玉', 'num': 808, 'height': 184, 'weight': 47, 'gender': '女', 'age': 23},?
{'name': '小強(qiáng)', 'num': 809, 'height': 154, 'weight': 67, 'gender': '男', 'age': 26},?
{'name': '小娟', 'num': 800, 'height': 153, 'weight': 49, 'gender': '女', 'age': 30},?
{'name': '小明', 'num': 804, 'height': 174, 'weight': 70, 'gender': '男', 'age': 30}
]

7、orient =‘index’

orient =‘index’ ,轉(zhuǎn)化后的字典形式:{index(值) :{column(列名) : value(值)}}

dic1 = df.to_dict('index')
print(dic1)

結(jié)果:

{
0: {'name': '張三', 'num': 802, 'height': 183, 'weight': 87, 'gender': '男', 'age': 25},?
1: {'name': '李四', 'num': 807, 'height': 161, 'weight': 60, 'gender': '男', 'age': 30},?
2: {'name': '王二', 'num': 801, 'height': 163, 'weight': 71, 'gender': '男', 'age': 25},?
3: {'name': '麻子', 'num': 803, 'height': 163, 'weight': 74, 'gender': '男', 'age': 26},?
4: {'name': '小紅', 'num': 806, 'height': 156, 'weight': 45, 'gender': '女', 'age': 27},?
5: {'name': '小蘭', 'num': 805, 'height': 186, 'weight': 50, 'gender': '女', 'age': 20},?
6: {'name': '小玉', 'num': 808, 'height': 184, 'weight': 47, 'gender': '女', 'age': 23},?
7: {'name': '小強(qiáng)', 'num': 809, 'height': 154, 'weight': 67, 'gender': '男', 'age': 26},?
8: {'name': '小娟', 'num': 800, 'height': 153, 'weight': 49, 'gender': '女', 'age': 30},?
9: {'name': '小明', 'num': 804, 'height': 174, 'weight': 70, 'gender': '男', 'age': 30}
}

8、指定列為key生成字典的實(shí)現(xiàn)步驟(按行)

1、 set_index用于將想設(shè)置為key的列設(shè)置為數(shù)據(jù)框索引

 df.set_index("name", drop=True, inplace=True)
 # 其中 drop=True去重,inplace=True在原數(shù)據(jù)上更改

結(jié)果:

2、使用orient=index參數(shù)將索引用作字典鍵。

dictionary = df.to_dict(orient="index")
print(dictionary)

結(jié)果

{
'張三': {'num': 802, 'height': 183, 'weight': 87, 'gender': '男', 'age': 25},?
'李四': {'num': 807, 'height': 161, 'weight': 60, 'gender': '男', 'age': 30},?
'王二': {'num': 801, 'height': 163, 'weight': 71, 'gender': '男', 'age': 25},?
'麻子': {'num': 803, 'height': 163, 'weight': 74, 'gender': '男', 'age': 26},?
'小紅': {'num': 806, 'height': 156, 'weight': 45, 'gender': '女', 'age': 27},?
'小蘭': {'num': 805, 'height': 186, 'weight': 50, 'gender': '女', 'age': 20},?
'小玉': {'num': 808, 'height': 184, 'weight': 47, 'gender': '女', 'age': 23},?
'小強(qiáng)': {'num': 809, 'height': 154, 'weight': 67, 'gender': '男', 'age': 26},?
'小娟': {'num': 800, 'height': 153, 'weight': 49, 'gender': '女', 'age': 30},?
'小明': {'num': 804, 'height': 174, 'weight': 70, 'gender': '男', 'age': 30}
}

3、將步驟1、2合起來(lái)寫(xiě)也可以,這里不修改源數(shù)據(jù)

dictionary = df.set_index("name", drop=True).to_dict(orient="index")

9、指定列為key,value生成字典的實(shí)現(xiàn)

1、指定一個(gè)列為key,一列為value

dictionary  = df.set_index("name")["num"].to_dict()
print(dictionary)

結(jié)果

{
'張三': 802,?
'李四': 807,?
'王二': 801,?
'麻子': 803,?
'小紅': 806,?
'小蘭': 805,?
'小玉': 808,?
'小強(qiáng)': 809,?
'小娟': 800,?
'小明': 804
}

2、指定多個(gè)列為key,一列為value

dictionary  = df.set_index(["name","num"])["weight"].to_dict()
print(dictionary)

結(jié)果:

{
('張三', 802): 87,?
('李四', 807): 60,?
('王二', 801): 71,?
('麻子', 803): 74,?
('小紅', 806): 45,?
('小蘭', 805): 50,?
('小玉', 808): 47,?
('小強(qiáng)', 809): 67,?
('小娟', 800): 49,?
('小明', 804): 70
}

3、指定一個(gè)列為key,多列為value

方法1(速度慢)

dictionary = {c0:[c1,c2] for c0,c1,c2 in zip(df['name'],df['num'],df['weight'])} 
print(dictionary)

方法2(速度快)

dictionary = df[["name",'num','weight']].set_index('name').T.to_dict('list')
print(dictionary)

結(jié)果:

{
'張三': [802, 87],?
'李四': [807, 60],?
'王二': [801, 71],?
'麻子': [803, 74],?
'小紅': [806, 45],?
'小蘭': [805, 50],?
'小玉': [808, 47],?
'小強(qiáng)': [809, 67],?
'小娟': [800, 49],?
'小明': [804, 70]
}

4、 指定多列為key,多列為value

dictionary = df[["name",'num','weight',"age"]].set_index(['name','num']).T.to_dict('list')
print(dictionary)

結(jié)果:

{
('張三', 802): [87, 25],?
('李四', 807): [60, 30],?
('王二', 801): [71, 25],?
('麻子', 803): [74, 26],?
('小紅', 806): [45, 27],?
('小蘭', 805): [50, 20],?
('小玉', 808): [47, 23],?
('小強(qiáng)', 809): [67, 26],?
('小娟', 800): [49, 30],?
('小明', 804): [70, 30]
}

參考https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_dict.html

總結(jié)

原文鏈接:https://blog.csdn.net/m0_43609475/article/details/125328938

欄目分類(lèi)
最近更新