-1

我有一个包含两列 A 和 B 的数据框。首先,我需要使用步骤 1 从 1 到 11、(1,2)、(2,3)....(10,11) 制作空箱。然后从原始数据框中检查 B 列的值是否大于 3,然后在 B 列大于 3 之前获取“A”列 2 行的值。

Here is example dataframe :
df=pd.DataFrame({'A':[1,8.5,5.2,7,8,9,0,4,5,6],'B':[1,2,2,2,3.1,3.2,3,2,1,2]})

Required output 1:
df_out1=pd.DataFrame({'Value_A':[8.5,5.2]})

Required_output_2:

df_output2:

Bins    count
(1 2)    0    
(2,3)    0
(3,4)    0
(4,5)    0
(5,6)    1
(6,7)    0
(7,8)  0
(8,9)  1  
(9,10)  0
(10,11) 0
4

1 回答 1

0

您可以对移位系列进行索引以在“A”满足某些条件之前获取两行,例如

out1 = df['A'].shift(3)[df['B'] > 3]

您想要对 bin 执行的操作称为直方图。您可以使用 numpy 轻松做到这一点

count, bin_edges = np.histogram(out1, bins=[i for i in range(1, 12)])

out2 = pd.DataFrame({'bin_lo': bin_edges[:-1], 'bin_hi': bin_edges[1:], 'count': count})

这里 'bin_lo' 和 'bin_hi' 是 bin 的下限和上限。

于 2021-07-20T07:43:03.423 回答