numpy 为什么loc在我的Pandas Dataframe中添加NaN行?

prdp8dxp  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(133)

我用的是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]

如果我这样做,为什么在数据集的末尾添加一个新行?一行,除了“X2 House”之外,所有NaN。我一定是做错了什么,但我不知道为什么。

8zzbczxx

8zzbczxx1#

IIUC,如果你想把pd.cut列的值赋给X2 house age列,你可以简单地做:

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

图纸:

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

相关问题