mgu*_*che 3 python numpy vectorization pandas
可能是一个非常简单的问题,但我无法提出解决方案.我有一个包含9列和~100000行的数据框.从图像中提取数据,使得两列('row'和'col')指的是数据的像素位置.如何创建一个numpy数组A,使得行和列指向另一列中的另一个数据条目,例如'grumpiness'?
A[row, col]
# 0.1232
Run Code Online (Sandbox Code Playgroud)
我想避免使用for循环或类似的东西.
一种非常快速和直接的方法是使用pivot_table:
>>> df
row col grumpiness
0 5 0 0.846412
1 0 1 0.703981
2 3 1 0.212358
3 0 2 0.101585
4 5 1 0.424694
5 5 2 0.473286
>>> df.pivot_table('grumpiness', 'row', 'col', fill_value=0)
col 0 1 2
row
0 0.000000 0.703981 0.101585
3 0.000000 0.212358 0.000000
5 0.846412 0.424694 0.473286
Run Code Online (Sandbox Code Playgroud)
请注意,如果缺少任何完整的行/列,它会将它们排除在外,如果重复任何行/列对,它将平均结果。也就是说,对于较大的数据集,这通常比基于索引的方法快得多。
你可以这样做 -
# Extract row and column information
rowIDs = df['row']
colIDs = df['col']
# Setup image array and set values into it from "grumpiness" column
A = np.zeros((rowIDs.max()+1,colIDs.max()+1))
A[rowIDs,colIDs] = df['grumpiness']
Run Code Online (Sandbox Code Playgroud)
样品运行 -
>>> df
row col grumpiness
0 5 0 0.846412
1 0 1 0.703981
2 3 1 0.212358
3 0 2 0.101585
4 5 1 0.424694
5 5 2 0.473286
>>> A
array([[ 0. , 0.70398113, 0.10158488],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0.21235838, 0. ],
[ 0. , 0. , 0. ],
[ 0.84641194, 0.42469369, 0.47328598]])
Run Code Online (Sandbox Code Playgroud)