python pandas数据帧列转换为dict键和值 [英] python pandas dataframe columns convert to dict key and value
本文介绍了python pandas数据帧列转换为dict键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
数据框:
b $ b co tp
DE湖10 7
森林20 5
FR湖30 2
森林40 3
需要将区域定义为关键字,计数为dict中的值。谢谢你提前。
解决方案
如果 lakes
是你的 DataFrame
,您可以执行类似
area_dict = dict(zip(lakes.area ,lakes.count))
A python pandas dataframe with multi-columns, there are only two columns needed for dict. One as dict's keys and another as dict's values. How can I do that?
Dataframe:
area count
co tp
DE Lake 10 7
Forest 20 5
FR Lake 30 2
Forest 40 3
Need to define area as key, count as value in dict. thank you in advance.
解决方案
If lakes
is your DataFrame
, you can do something like
area_dict = dict(zip(lakes.area, lakes.count))
这篇关于python pandas数据帧列转换为dict键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文