How to treat NULL as a normal string with pandas?

后端 未结 4 1500
情书的邮戳
情书的邮戳 2020-12-09 15:34

I have a csv-file with a column with strings and I want to read it with pandas. In this file the string null occurs as an actual value and should not be regarde

4条回答
  •  情深已故
    2020-12-09 16:13

    You can specify a converters argument for the string column.

    pd.read_csv(StringIO(data), converters={'strings' : str})
    
      strings  numbers
    0     foo        1
    1     bar        2
    2    null        3
    

    This will by-pass pandas' automatic parsing.


    Another option is setting na_filter=False:

    pd.read_csv(StringIO(data), na_filter=False)
    
      strings  numbers
    0     foo        1
    1     bar        2
    2    null        3
    

    This works for the entire DataFrame, so use with caution. I recommend first option if you want to surgically apply this to select columns instead.

提交回复
热议问题