I am using this csv.
import pandas as pd
import numpy as np
real_estate = pd.read_csv('real_estate.csv',index_col=0)
buckets = pd.cut(real_estate['X2 house age'],4,labels=False)
for i in range(len(real_estate['X2 house age'])):
real_estate.loc[i,'X2 house age'] = buckets[i]
It gives me:
KeyError: 0
for the line real_estate.loc[i,'X2 house age'] = buckets[i] it fails just at the first iteration
Why do I need to change the line to buckets = pd.cut(real_estate['X2 house age'],4,labels=False).to_numpy() to make it work?
You shouldn't need the loop, just use:
Your current approach is failing because you don't have a range index starting from
0. Thus, when assigning to index0,1, …, pandas is not finding the correct index and shifts the data.Output: