How to make function to do iteration over row in pandas?

I have a dataframe like this:

data = [[2,10,1],[3,20,3],[4,30,5],[5,40,7],[6,50,9],[7,60,11],[8,70,13], [9,80,15],[10,90,17],[11,100,19], [12,110,21],[13,120,23],[14,130,25],[15,140,27],[16,150,29],[17,160,31],[18,170,33]]

df = pd.DataFrame(data, columns = ['A', 'B', 'C'])

Before Processing

However, I wanted to process the data like this:

def formula(i): for i in range(len(df)): df.loc[i+12]-df.loc[i]

df['D'] = df['C'].apply(formula)

but showing error. my intention is to get something like this:

Column D = data row3-row 1 iterate until last

After Processing

Upvotes: 0

Views: 42

Answers (1)

Marat
Marat

Reputation: 15738

df['D'] = df['C'] - df['C'].shift(3)

Upvotes: 1

Related Questions