[英]pandas panel data update
I am using panel datastructure in pandas for storing a 3d panel. 我在pandas中使用面板数据结构来存储3d面板。
T=pd.Panel(data=np.zeros((n1,n2,n3)),items=n1_label, major_axis=n2_label, minor_axis=n3_label)
Later on, I am trying to update (increment) the values stored at individual locations inside a loop. 稍后,我试图更新(增加)存储在循环内各个位置的值。 Currently I am doing: 目前我在做:
u=T.get_value(n1_loop,n2_loop,n3_loop)
T.set_value(n1_loop,n2_loop,n3_loop,u+1)
My question- is this the simplest way? 我的问题 - 这是最简单的方法吗? Is there any other simpler way? 还有其他更简单的方法吗? The following dont work: 以下不起作用:
T[n1_loop,n2_loop,n3_loop] +=1
or 要么
T[n1_loop,n2_loop,n3_loop] = T[n1_loop,n2_loop,n3_loop] +1
TL;DR TL; DR
T.update(T.loc[[n1_loop], [n2_loop], [n3_loop]].add(1))
The analogous exercise for a DataFrame would be to assign with loc
DataFrame的类似练习是使用loc
进行分配
df = pd.DataFrame(np.zeros((5, 5)), list('abcde'), list('ABCDE'), int)
df.loc['b':'d', list('AE')] += 1
df
Exact pd.Panel
analog generates an Error 精确的pd.Panel
模拟生成错误
pn = pd.Panel(np.zeros((5, 5, 2)), list('abcde'), list('ABCDE'), list('XY'), int)
pn.loc['b':'d', list('AE'), ['X']] += 1
pn
NotImplementedError: cannot set using an indexer with a Panel yet!
But we can still slice it 但我们仍然可以分割它
pn = pd.Panel(np.zeros((5, 5, 2)), list('abcde'), list('ABCDE'), list('XY'), int)
pn.loc['b':'d', list('AE'), ['X']]
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 2 (major_axis) x 1 (minor_axis)
Items axis: b to d
Major_axis axis: A to E
Minor_axis axis: X to X
And we can use the update
method 我们可以使用update
方法
pn.update(pn.loc['b':'d', list('AE'), ['X']].add(1))
Which we can see did stuff if we use the to_frame
method 如果我们使用to_frame
方法,我们可以看到这些内容
pn.to_frame().astype(int)
OR 要么
pn.loc[:, :, 'X'].astype(int).T
YOUR CASE 你的情况
This should work 这应该工作
T.update(T.loc[[n1_loop], [n2_loop], [n3_loop]].add(1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.