Remove NaN from pandas series

前端 未结 3 1433
抹茶落季
抹茶落季 2020-11-29 05:05

Is there a way to remove a NaN values from a panda series? I have a series that may or may not have some NaN values in it, and I\'d like to return a copy of the series with

3条回答
  •  温柔的废话
    2020-11-29 05:24

    If you have a pandas serie with NaN, and want to remove it (without loosing index):

    serie = serie.dropna()

    # create data for example
    data = np.array(['g', 'e', 'e', 'k', 's']) 
    ser = pd.Series(data)
    ser.replace('e', np.NAN)
    print(ser)
    
    0      g
    1    NaN
    2    NaN
    3      k
    4      s
    dtype: object
    
    # the code
    ser = ser.dropna()
    print(ser)
    
    0    g
    3    k
    4    s
    dtype: object
    

提交回复
热议问题