How to treat NULL as a normal string with pandas?

后端 未结 4 1491
情书的邮戳
情书的邮戳 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:00

    The reason this happens is that the string 'null' is treated as NaN on parsing, you can turn this off by passing keep_default_na=False in addition to @coldspeed's answer:

    In[49]:
    data = u'strings,numbers\nfoo,1\nbar,2\nnull,3'
    df = pd.read_csv(io.StringIO(data), keep_default_na=False)
    df
    
    Out[49]: 
      strings  numbers
    0     foo        1
    1     bar        2
    2    null        3
    

    The full list is:

    na_values : scalar, str, list-like, or dict, default None

    Additional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. By default the following values are interpreted as NaN: ‘’, ‘#N/A’, ‘#N/A N/A’, ‘#NA’, ‘-1.#IND’, ‘-1.#QNAN’, ‘-NaN’, ‘-nan’, ‘1.#IND’, ‘1.#QNAN’, ‘N/A’, ‘NA’, ‘NULL’, ‘NaN’, ‘n/a’, ‘nan’, ‘null’.

提交回复
热议问题