Home > Net >  Why cannot use loc(index 1,'col name') in a for loop in pandas?
Why cannot use loc(index 1,'col name') in a for loop in pandas?

Time:09-16

I want to calculate difference between rows in python. I konw i can use diff() to do that. But i want to try to use a "for loop". I try the following codes, but i get error "KeyError: 2" in line if pdf.loc[i 1,'A'] != pdf.loc[i,'A']:

If i want to do the following calculation, how can i do it, please Help.

print(loc[2,'A'] * loc[1,'A'] 3)

print(loc[3,'A'] * loc[2,'A'] 3)

print(loc[4,'A'] * loc[3,'A'] 3)

...

print(loc[i,'A'] * loc[i-1,'A'] 3)

The code which showed error with "KeyError: 2"

for i, dura in pdf.iterrows():

     if i < pdf.shape[0]:
         if pdf.loc[i 1,'A'] != pdf.loc[i,'A']:
            print("a")
         else:
            print("b")
     else:
         print("finished")

CodePudding user response:

iterrows() is one of the slowest ways to iterate through the pandas dataframe. Use itertuples() instead (there are other faster options )

CodePudding user response:

use iloc you can, enumerate(df,2) start from 2 as you write in the question.

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5]
})

for i, dura in enumerate(df,2):
    print(df.iloc[i]['A']*df.iloc[i-1]['A'] 3)

Output:

9  # 3 * 2   3
15 # 4 * 3   3
23 # 5 * 4   3
  • Related