Find difference between column X of first row with column Y of second Row.

huangapple go评论65阅读模式
英文:

Find difference between column X of first row with column Y of second Row

问题

我对pandas很陌生,我在pandas中有以下数据集:

Find difference between column X of first row with column Y of second Row.

这里我想找到第一行的上限与第二行的下限之间的差异。

简而言之,我想找到df[0].upper和df1.lower之间的差异。

我可以通过循环遍历行来实现这一点,但数据集很大,我有时间限制。是否有任何预先构建的pandas/numpy函数可以帮助我更快地完成这个任务?

英文:

I am very new to pandas,
i have following data set in pandas:

Find difference between column X of first row with column Y of second Row.

here i want to find the difference between the upper of first row with lower of second row

in short i want to find difference between
df[0].upper and df1.lower

i can acheive this by looping through the rows but the data set is huge and i have time constraint. is there any pre-built pandas/numpy function that will help me do this quicker?

答案1

得分: 1

使用shift(-1),然后只需相减

df['difference'] = df['upper'] - df['lower'].shift(-1)
英文:

Use shift(-1) and then just subtract

df['difference'] = df['upper'] - df['lower'].shift(-1)

huangapple
  • 本文由 发表于 2023年5月11日 00:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定