filling NAN and converting to int pandas

不打扰是莪最后的温柔 提交于 2019-12-11 17:35:31

问题


I have a dataframe of integers. Preview (starts from 3 due to first 3 rows removal):

The original data in the 'pixel1' column is int, but the NAN there forced it to float.

I tried to fix it with:

X_train.fillna(method='ffill', inplace=True)
X_train = X_train.astype(int)
print(X_train.head())

that results in:

  • can I get the datatype of the value the fillna is using?
  • is there a better way to do so? (better = to skip the astype step, as the data is int originally - I planted the NAN in the file and that caused the int to float unwanted data conversion...)

回答1:


I suggest use ffill with bfill for back filling if possible some NaNs in first row:

X_train = X_train.ffill().bfill().astype(int)

If not:

X_train = X_train.ffill().astype(int)


来源:https://stackoverflow.com/questions/50484145/filling-nan-and-converting-to-int-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!