熊猫groupby-计算到相对点的距离

时间:2019-10-14 18:22:35

标签: python pandas pandas-groupby

让我们说我看起来像这样

new SecretKeySpec(secret.getBytes ( Charsets.UTF_8 ), ALGORITHM)

我想做的是创建一个新列,该列是Number与ref中True的区别。因此,对于A组,True是最后一个,因此该列将显示为-4,-3,-2,-1,0。我一直在想做以下事情:

df = pd.DataFrame({'Event':['A','A','A','A', 'A' ,'B','B','B','B','B'],  'Number':[1,2,3,4,5,6,7,8,9,10],'Ref':[False,False,False,False,True,False,False,False,True,False]})

这似乎可以为每个组正确计算,但是我不确定如何将结果加入df。

3 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

@bd_local = {}
@bd_remote = {}
@c_local = PG.connect(:hostaddr => '10.11.12.36', :port => 5432, :dbname => "cnet", :user => "cuser", :connect_timeout => 90)

#get adress from local bd to acess remote bd
@bdlocal_ip_remote.each do |k|
  @ips = {}
  @ips = k

#conect in remote servers
  @c_remote = PG.connect(:hostaddr => @ips["host"], :port => 5432, :dbname => "cdbs", :user => "cnet", :connect_timeout => 90)

#GET ALL DATA and set to hash @bd_remote
  @bd_remote = @c_remote.exec("SELECT id,name,phone_number,active FROM trunks")
    @bd_remote.each do |q|
    @ipx = {}
    @ipx = q
  #puts "Server: #{@ips["host"]} - ID: #{@ipx["id"]} - Phone: - #{@ipx["phone"]} - Status #{@ipx["active"]}"
  end
end

输出

df["new"] = df.Number - df.Number[df.groupby('Event')['Ref'].transform('idxmax')].reset_index(drop=True)
print(df)

此: Event Number Ref new 0 A 1 False -4 1 A 2 False -3 2 A 3 False -2 3 A 4 False -1 4 A 5 True 0 5 B 6 False -3 6 B 7 False -2 7 B 8 False -1 8 B 9 True 0 9 B 10 False 1 填充按组查找索引,其中Ref为df.groupby('Event')['Ref'].transform('idxmax')。基本上,它会找到最大值的索引,因此在给定True = 1和False = 0的情况下,它会找到True值的索引。

答案 1 :(得分:2)

以您的情况

results = parentNode.get(GEOMETRY);

答案 2 :(得分:1)

尝试where和grouby变换first

s = df.Number.where(df.Ref).groupby(df.Event).transform('first')
df.Number - s

Out[319]:
0   -4.0
1   -3.0
2   -2.0
3   -1.0
4    0.0
5   -3.0
6   -2.0
7   -1.0
8    0.0
9    1.0
Name: Number, dtype: float64