我有一个包含四列的DataFrame.我想将此DataFrame转换为python字典.我想要第一列keys的元素和同一行中其他列的元素values.
数据帧:
ID A B C
0 p 1 3 2
1 q 4 3 2
2 r 4 0 9
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的:
字典:
{'p': [1,3,2], 'q': [4,3,2], 'r': [4,0,9]}
Run Code Online (Sandbox Code Playgroud) 一个包含多列的python pandas数据帧,dict只需要两列.一个是dict的键,另一个是dict的值.我怎样才能做到这一点?
数据帧:
area count
co tp
DE Lake 10 7
Forest 20 5
FR Lake 30 2
Forest 40 3
Run Code Online (Sandbox Code Playgroud)
需要将区域定义为键,在dict中计为值.先感谢您.
我打算从数据框中获取一个列名作为键的字典.
假设我有一个数据帧:
a b
0 ac dc
1 ddd fdf
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
{a : ac, b : dc}
Run Code Online (Sandbox Code Playgroud)
我希望它一行一行地完成.任何形式的帮助将不胜感激.在这里,我希望列名称作为结果字典中的键.
我还处于 Python 学习的早期阶段。如果这个问题听起来很愚蠢,请提前道歉。
我有这组数据(表格格式),我想向其中添加几个计算列。基本上我有一些位置 lon/lat 和目的地 lon/lat,以及各自的数据时间,我正在计算每对之间的平均速度。
示例数据如下所示:
print(data_all.head(3))
id lon_evnt lat_evnt event_time \
0 1 -179.942833 41.012467 2017-12-13 21:17:54
1 2 -177.552817 41.416400 2017-12-14 03:16:00
2 3 -175.096567 41.403650 2017-12-14 09:14:06
dest_data_generate_time lat_dest lon_dest \
0 2017-12-13 22:33:37.980 37.798599 -121.292193
1 2017-12-14 04:33:44.393 37.798599 -121.292193
2 2017-12-14 10:33:51.629 37.798599 -121.292193
address_fields_dest \
0 {'address': 'Nestle Way', 'city': 'Lathrop...
1 {'address': 'Nestle Way', 'city': 'Lathrop...
2 {'address': 'Nestle Way', 'city': 'Lathrop...
Run Code Online (Sandbox Code Playgroud)
然后我将 lon/lat 压缩在一起:
data_all['ping_location'] = list(zip(data_all.lon_evnt, data_all.lat_evnt))
data_all['destination'] = …Run Code Online (Sandbox Code Playgroud) 过去一小时我一直在尝试这个,但几乎没有成功
我想知道如何将这个数据帧设置为字典
DF
Country Continent
1 Europe
2 Europe
3 Asia
4 Australia
Run Code Online (Sandbox Code Playgroud)
期望
{'1': 'Europe',
'2': 'Europe',
'3': 'Asia',
'5': 'Australia'}
Run Code Online (Sandbox Code Playgroud)
我厌倦了像x10不同的代码变体,但它没有用
df.set_index('Country', inplace=True)
df = df.to_dict('dict')
Run Code Online (Sandbox Code Playgroud) 如何使用 Dataframe 构建字典。
df
Vendor Project
0 John Cordoba
1 Saul Pampa
2 Peter La Plata
3 John Federal District
4 Lukas Salta
5 Thomas Rio Grande
6 Peter Rio Salado
7 Peter Bahia Blanca
Run Code Online (Sandbox Code Playgroud)
我需要的结果是一本字典,其中供应商作为键,项目作为项目。
我有一个数据框,其中有两种产品的属性和值。
PRODUCT ATTRIBUTE VALUES
prod1 Attr1 A
prod1 Attr2 B
prod1 Attr3 C
prod1 Attr4 D
prod2 Attr1 E
prod2 Attr2 F
prod2 Attr3 G
prod2 Attr4 H
Run Code Online (Sandbox Code Playgroud)
如何将其转换为如下所示的字典列表的字典?
{'prod1':[{'Attr1':A, 'Attr2':B, 'Attr3':C, 'Attr4':D}], 'prod2':[{'Attr1':E, 'Attr2':F, 'Attr3':G, 'Attr4':H}]}
Run Code Online (Sandbox Code Playgroud)