skip to Main Content
ValueError                                Traceback (most recent call last)
<ipython-input-8-7c5fcf8552e0> in <cell line: 1>()
----> 1 df2 = pd.read_json('/content/drive/MyDrive/Hackathon project/PS 1,2/no_pii_action_history.json')

6 frames
/usr/local/lib/python3.10/dist-packages/pandas/io/json/_json.py in _parse_no_numpy(self)
   1319         if orient == "columns":
   1320             self.obj = DataFrame(
-> 1321                 loads(json, precise_float=self.precise_float), dtype=None
   1322             )
   1323         elif orient == "split":

ValueError: Unexpected character found when decoding 'NaN'

we tried 2 methods

1)

df2 = pd.read_json('/content/drive/MyDrive/Hackathon project/PS 1,2/no_pii_action_history.json')
import json

def handle_nan(value):
    if value == "nan":  # replace "nan" with the appropriate representation of NaN in your data
        return float('nan')
    return value

# Specify the file path
file_path = '/content/drive/MyDrive/Hackathon project/PS 1,2/no_pii_action_history.json'

# Open the file and load JSON data with custom handling
with open(file_path, 'r') as file:
    data = json.load(file, object_hook=handle_nan)

both methods are not working

2

Answers


  1. You can use None or another sentinel value to represent NaN
    Try this:

    import json
    
    def handle_nan(value):
        if value == "nan":
            return None  
        return value
    
    file_path = '/content/drive/MyDrive/Hackathon project/PS 1,2/no_pii_action_history.json'
    
    with open(file_path, 'r') as file:
        data = json.load(file, object_hook=handle_nan)
    
    Login or Signup to reply.
  2. Did you try to replace NaN with null in Json file.
    It’s hard to respond without seeing the json file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search