Convert Pandas column containing NaNs to dtype `int`

后端 未结 17 2207
终归单人心
终归单人心 2020-11-22 11:18

I read data from a .csv file to a Pandas dataframe as below. For one of the columns, namely id, I want to specify the column type as int. The probl

相关标签:
17条回答
  • 2020-11-22 12:05

    It is now possible to create a pandas column containing NaNs as dtype int, since it is now officially added on pandas 0.24.0

    pandas 0.24.x release notes Quote: "Pandas has gained the ability to hold integer dtypes with missing values

    0 讨论(0)
  • 2020-11-22 12:06
    import pandas as pd
    
    df= pd.read_csv("data.csv")
    df['id'] = pd.to_numeric(df['id'])
    
    0 讨论(0)
  • 2020-11-22 12:08

    My use case is munging data prior to loading into a DB table:

    df[col] = df[col].fillna(-1)
    df[col] = df[col].astype(int)
    df[col] = df[col].astype(str)
    df[col] = df[col].replace('-1', np.nan)
    

    Remove NaNs, convert to int, convert to str and then reinsert NANs.

    It's not pretty but it gets the job done!

    0 讨论(0)
  • 2020-11-22 12:08

    use pd.to_numeric()

    df["DateColumn"] = pd.to_numeric(df["DateColumn"])
    

    simple and clean

    0 讨论(0)
  • 2020-11-22 12:09

    Try this:

    df[['id']] = df[['id']].astype(pd.Int64Dtype())

    If you print it's dtypes, you will get id Int64 instead of normal one int64

    0 讨论(0)
提交回复
热议问题