skip to Main Content

I have a data frame that contains a column called identifiers and i need to change this to a json list.

list_identifiers=df['identifier'].tolist() listjson=json.dumps(list_identifiers)

It looks like some Nan values are invalid and i get this error when i am running a query with the list.

DETAIL:  Token "NaN" is invalid.
CONTEXT:  JSON data, line 1: ...E00B8KQN827", "US0530151036", "CA82509L1076", NaN...

Can you help me remove the NaN from the json list? Thanks a lot!

3

Answers


  1. For removing NAN from a list, did you try numpy.nan object?
    I mean do it this way:

    import numpy
    list_identifiers = df['identifier'].tolist()
    list_identifiers.remove(numpy.nan)
    

    Furthermore, as I know there is also a dropna method in Pandas library you can use:

     list_identifiers = df['identifier'].dropna().tolist()
    
    Login or Signup to reply.
  2. json_list = json.dumps(df['identifier'].dropna().tolist())
    
    Login or Signup to reply.
  3. pandas to_json should convert "NaN" to "null" for you, which are JSON compatible:

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame({"identifiers": [1, 2, np.nan, pd.NA]})
    
    df["identifiers"].to_json(orient="values")
    # output: '[1,2,null,null]'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search