大熊猫两行比较
问题描述:
有什么方法可以比较两行熊猫吗?大熊猫两行比较
我会在sql中这样做。
abc = df[df['type'] == 'abc']
def = df[df['type'] == 'def']
,我坚持至于如何去这样做:
Select * from table t1, table t2 where t1.price - t2.price > 10 and t1.type = 'abc' and t2.type = 'def'
我能想到的最好的办法是在此基础上减去大熊猫数据帧行?
喜欢的东西
price type
10 abc
10 def
30 abc
15 def
应该返回行
10 def
30 abc
15 def
答
Aceminer,我还没有完全清楚你在找什么的比较,但我设置了类似的东西。这里需要注意的一点是,这段代码将一行与以下内容进行比较,以便最后一行不显示,因为没有任何可以比较的内容。
df = pd.DataFrame({'price':[10,10,30,15], 'type':['abc','def','abc','def']})
price type
0 10 abc
1 10 def
2 30 abc
3 15 def
筛选:
df = df[(df['type'] != df['type'].shift(-1)) & (abs(df['price'] - df['price'].shift(-1)) > 10)]
结果:
price type
1 10 def
2 30 abc
你能加样日期与期望的输出?谢谢。 – jezrael
我有点困惑't1'和't2'是不同的表?如果是,为什么不在样本中? – jezrael
我应该提到t1和t2是同一张表 – aceminer