簡體   English   中英

熊貓從字典創建數據幀將元組轉換為索引

[英]Pandas create dataframe from dict converting tuple to index

有沒有方便的方法實現一個函數make_dataframe ,使用如下

mydict = {
    ('tom', 'gray') : [1,2,3,4,5],
    ('bill', 'ginger') : [6,7,8,9,10],
}

make_dataframe(mydict, tupleLabels=['catname', 'catcolor'], valueLabel='weight')

預期結果

| catname | catcolor | weight |
| tom | gray | 1 |
| tom | gray | 2 |
| tom | gray | 3 |
| tom | gray | 4 |
| tom | gray | 5 |
| bill | ginger | 6 |
| bill | ginger | 7 |
| bill | ginger | 8 |
| bill | ginger | 9 |
| bill | ginger | 10 |

聽起來不太難,我只是不想重新發明輪子

您可以使用數據幀創建自己的功能unstack重命名使用的標簽后rename_axis

def make_dataframe(dictionary , tupleLabels , valueLabel):
    return (pd.DataFrame(dictionary).rename_axis(tupleLabels,axis=1)
            .unstack().reset_index(tupleLabels,name=valueLabel))

out = make_dataframe(mydict, tupleLabels=['catname', 'catcolor'], valueLabel='weight')

print(out)

  catname catcolor  weight
0     tom     gray       1
1     tom     gray       2
2     tom     gray       3
3     tom     gray       4
4     tom     gray       5
0    bill   ginger       6
1    bill   ginger       7
2    bill   ginger       8
3    bill   ginger       9
4    bill   ginger      10

您的字典格式錯誤,無法輕松轉換為 Pandas DataFrame。

我建議執行以下操作:


mydict = {
    ('tom', 'gray') : [1,2,3,4,5],
    ('bill', 'ginger') : [6,7,8,9,10],
}

l = [ [ k[0], k[1], val ] for k, v in mydict.items() for val in v ]

df = pd.DataFrame(l, columns=['catname', 'catcolor', 'weight'])

其中產生:

  catname catcolor  weight
0     tom     gray       1
1     tom     gray       2
2     tom     gray       3
3     tom     gray       4
4     tom     gray       5
5    bill   ginger       6
6    bill   ginger       7
7    bill   ginger       8
8    bill   ginger       9
9    bill   ginger      10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM