Why does loc adds a NaN row in my Pandas Dataframe?

35 views Asked by At

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).to_numpy()

for i in range(len(real_estate['X2 house age'])):
    real_estate.loc[i,'X2 house age'] = buckets[i]

Why if I do this a new row is added at the end of the dataset? A row with all NaN except 'X2 House Age'.... I'm must doing something wrong but I don't know why.

1

There are 1 answers

0
Andrej Kesely On BEST ANSWER

IIUC, if you want to assign the values from pd.cut to X2 house age column, you can simply do:

real_estate["X2 house age"] = pd.cut(real_estate["X2 house age"], 4, labels=False)
print(real_estate.head())

Prints:

    X1 transaction date  X2 house age  X3 distance to the nearest MRT station  X4 number of convenience stores  X5 latitude  X6 longitude  Y house price of unit area
No                                                                                                                                                                   
1              2012.917             2                                84.87882                               10     24.98298     121.54024                        37.9
2              2012.917             1                               306.59470                                9     24.98034     121.53951                        42.2
3              2013.583             1                               561.98450                                5     24.98746     121.54391                        47.3
4              2013.500             1                               561.98450                                5     24.98746     121.54391                        54.8
5              2012.833             0                               390.56840                                5     24.97937     121.54245                        43.1