Converting a column within pandas dataframe from int to string

前端 未结 5 1440
南笙
南笙 2020-11-29 17:17

I have a dataframe in pandas with mixed int and str data columns. I want to concatenate first the columns within the dataframe. To do that I have to convert an int

5条回答
  •  孤街浪徒
    2020-11-29 17:48

    Warning: Both solutions given ( astype() and apply() ) do not preserve NULL values in either the nan or the None form.

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame([None,'string',np.nan,42], index=[0,1,2,3], columns=['A'])
    
    df1 = df['A'].astype(str)
    df2 =  df['A'].apply(str)
    
    print df.isnull()
    print df1.isnull()
    print df2.isnull()
    

    I believe this is fixed by the implementation of to_string()

提交回复
热议问题