有没有一种功能可以让人们减去带有数据框列的元素?例如,假设一个数据框列中具有以下值:
1
2
3
4
5
让我们假设1是元素i,2是元素i + 2,依此类推。一个人如何从i + 4中减去i?
答案 0 :(得分:0)
这对您有帮助吗?
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD')) #create random data frame
iterate_to=df.shape[0]-4 #specify how far you'll be iterating to
#iterating further than the number of rows will cause an error
differences=[] #create a list into which the calculated differences will be put
for i in range(0,iterate_to): #iterate through the rows 1 at a time
differences.append(df['A'][i+4]-df['A'][i]) #append the differences between i and i+4