[英]how to convert pandas dataframe and numpy array into dictionary?
I have the following pandas dataframe which looks like,我有以下熊猫数据框,它看起来像,
code comp name
0 A292340 디비자산운용 마이티 200커버드콜ATM레버리지
1 A291630 키움투자자산운용 KOSEF 코스닥150선물레버리지
2 A278240 케이비자산운용 KBSTAR 코스닥150선물레버리지
3 A267770 미래에셋자산운용 TIGER 200선물레버리지
4 A267490 케이비자산운용 KBSTAR 미국장기국채선물레버리지(합성 H)
And I like to make dictionary out of this which will look like,我喜欢用这个来制作字典,看起来像,
{'20180408' :{'A292340' : {comp : 디비자산운용}, {name : 마이티 200커버드콜ATM 레버리지}}}
Sorry about the data which is in foreign to you, but let me please ask.很抱歉这些数据对您来说是陌生的,但请让我问一下。 What I tried is like,
我试过的是,
values = [comp, name]
names = ['comp', 'name']
tmp = {names:values for names, values in zip(names, values)}
tpm = {code:values for values in zip(*tmp)}
aaaa = {date:c for c in zip(*tpm)}
print(aaaa)
aaaa is what I try to get.. and date is just simple list of date, from prior to todate. aaaa 是我试图得到的……而日期只是简单的日期列表,从之前到现在。 but when I run this, I got the error
但是当我运行这个时,我得到了错误
TypeError: unhashable type: 'numpy.ndarray'
Thank you in advance for your answer.预先感谢您的回答。
Is this what you want?这是你想要的吗? First, set
"code"
column as the index.首先,将
"code"
列设置为索引。 Then use to_dict
with "orient="index"
.然后将
to_dict
与"orient="index"
。
df.set_index("code").to_dict("index")
{'A267490': {'comp': '케이비자산운용', 'name': 'KBSTAR 미국장기국채선물레버리지(합성 H)'},
'A267770': {'comp': '미래에셋자산운용', 'name': 'TIGER 200선물레버리지'},
'A278240': {'comp': '케이비자산운용', 'name': 'KBSTAR 코스닥150선물레버리지'},
'A291630': {'comp': '키움투자자산운용', 'name': 'KOSEF 코스닥150선물레버리지'},
'A292340': {'comp': '디비자산운용', 'name': '마이티 200커버드콜ATM레버리지'}}
Using the argument "index"
will give the layout:使用参数
"index"
将给出布局:
{index -> {columnName -> valueOfTheColumn}}
Here since we set code
as the index, we have这里因为我们将
code
设置为索引,所以我们有
code -> {"comp" -> comp's value, "name" -> name's value}
'A267490': {'comp': '케이비자산운용', 'name': 'KBSTAR 미국장기국채선물레버리지(합성 H)'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.