Pandas Replace NaN with blank/empty string

前端 未结 8 1155
情话喂你
情话喂你 2020-11-29 15:10

I have a Pandas Dataframe as shown below:

    1    2       3
 0  a  NaN    read
 1  b    l  unread
 2  c  NaN    read

I want to remove the

8条回答
  •  天命终不由人
    2020-11-29 15:50

    Use a formatter, if you only want to format it so that it renders nicely when printed. Just use the df.to_string(... formatters to define custom string-formatting, without needlessly modifying your DataFrame or wasting memory:

    df = pd.DataFrame({
        'A': ['a', 'b', 'c'],
        'B': [np.nan, 1, np.nan],
        'C': ['read', 'unread', 'read']})
    print df.to_string(
        formatters={'B': lambda x: '' if pd.isnull(x) else '{:.0f}'.format(x)})
    

    To get:

       A B       C
    0  a      read
    1  b 1  unread
    2  c      read
    

提交回复
热议问题