[英]Convert a column from a pandas DataFrame to float with nan values
[英]How to change the data type of column of a Dataframe from float to int which has NaN values in it?
這不是重復的。 所有其他問題,您必須在構造DataFrame
時設置 dtype dtype='Int64'
或pd.Int64Dtype()
我有一個 dataframe 的列名score
,其中包含float,Nan
值。 我想將類型更改為int。 我用過
pd.to_numeric(df['score'], errors='coerce')
df['score'].map(int)
df['score'].astype('Int64')
但似乎沒有一種方法適用於此。 我知道我可以將NaN
值轉換為其他值,轉換然后再次更改值,但這是一種不好的做法。 有什么好的方法可以做到嗎?
你可以試試這個。。
import numpy as np
df['score'].replace('nan', np.nan).fillna(0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.