英文:
Find difference between column X of first row with column Y of second Row
问题
我对pandas很陌生,我在pandas中有以下数据集:
这里我想找到第一行的上限与第二行的下限之间的差异。
简而言之,我想找到df[0].upper和df1.lower之间的差异。
我可以通过循环遍历行来实现这一点,但数据集很大,我有时间限制。是否有任何预先构建的pandas/numpy函数可以帮助我更快地完成这个任务?
英文:
I am very new to pandas,
i have following data set in pandas:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论