In the following code, I expect that in each iteration of the loop, batch_df
index starts from 0, however, as you can see, in the second iteration the index starts from 3.
import pandas as pd
df = pd.read_csv('test.batch.csv')
print(df)
for i in range(0, len(df), 3):
print("\n------BATCH BEGIN")
batch_df = df.iloc[i:i+3]
print(batch_df)
print(batch_df.loc[0].at["Name"])
print("------BATCH END")
Output
ID Name Metric Value
0 0 K1 M1 10
1 0 K1 M2 5
2 0 K1 M3 10
3 1 K2 M1 20
4 1 K2 M2 10
5 1 K2 M3 15
------BATCH BEGIN
ID Name Metric Value
0 0 K1 M1 10
1 0 K1 M2 5
2 0 K1 M3 10
K1
------BATCH END
------BATCH BEGIN
ID Name Metric Value
3 1 K2 M1 20
4 1 K2 M2 10
5 1 K2 M3 15
ERROR ----> 7 print(batch_df.loc[0].at["Name"])
KeyError: 0
I can fix the code with this modification print(batch_df.loc[i].at["Name"])
, but I want to know why that happens and does that mean the first batch_df is not overwritten?