skip to Main Content

Starting with a DataFrame that has a form such as this

df = pl.DataFrame([{"SkuId":1}])
shape: (1, 1)
┌───────┐
│ SkuId │
│ ---   │
│ i64   │
╞═══════╡
│ 1     │
└───────┘

How can I write it to a JSON file with this format?

"SkuId": {"source": 1},  

2

Answers


  1. You can first transform the Polars DataFrame to a dictionary and then manipulate the dictionary to match the format you want before writing it to a JSON file.

    import polars as pl
    import json
    
    # Create the Polars DataFrame
    df = pl.DataFrame([{"SkuId": 1}])
    
    # Convert the DataFrame to a dictionary
    df_dict = df.to_dict(as_series=False)
    
    # Transform to the desired format
    output_dict = {key: {"source": value[0]} for key, value in df_dict.items()}
    
    # Write to a JSON file
    with open('output.json', 'w') as f:
        json.dump(output_dict, f, indent=4)
    
    print(output_dict)
    
    Login or Signup to reply.
  2. Polars has two (really 3) nested types. There are Structs which are like objects (aka {}) in JSON and Lists which are like Arrays (aka []).

    To get it to put JSON in the format you’re looking for, you need to make a struct.

    df.select(SkuId=pl.struct(source=pl.col('SkuId'))).write_json()
    
    [{"SkuId":{"source":1}}]
    

    Of course, if you put a file path inside write_json() then it’ll write it to the file instead of just printing it to stdout.

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