-2

I'm trying to convert a Polars dataframe to a JSON object, but I can't seem to find a way to change the format of it between row/col orientation. In Pandas, by default, it creates a column-oriented JSON object, and it can be changed easily, but in Polars it's row-oriented and I can't seem to find a way to change that, since the write_json method doesn't actually receives any argument other than the target filename if you want to save it directly.

Any idea how to achieve that?

3
  • 2
    Can you add a small example frame and show the actual output you require? Commented Feb 28 at 12:00
  • Casting a table into a json col-wise is a keyword definition, same as if i say i need to 'transpose' a matrix, or get its 'determinant', or 'eigenvalues'. If you "need an example" of what those things are, you clearly have no clue of the topic. I never said anything about the index, i CLEARLY stated what i wanted, to switch from row to col orientation, i didn't mention ANYTHING ELSE. You created the ambiguity by coming up with things i never mentioned, not me; under that logic, you could also say that i never mentioned if i wanted an extra key with my favourite colour, so, it's ambiguous. Commented Mar 3 at 10:40
  • 1
    @Ghost If it's trivial to understand then it should be trivial for you to figure out on your own. If you want people to help you (for free), then answer their questions when they ask for more information. That's how Stack Overflow works; help us help you. Commented Oct 28 at 18:31

2 Answers 2

4

It is possible to achieve all orientations in Polars as well. It just requires a bit more creativity and json.dumps() instead of write_json alone. I've compiled a mapping from Pandas different orientations to Polars equivalent below. Using the same dataframe for each example.

import polars as pl

df = pl.DataFrame({
    "A": [1, 2, 3],
    "B": [4, 5, 6]
})

Index one seems to be a bit overcomplicated than needed, maybe there is a better way of doing that?

Orientation Pandas Polars
Records orient="records" df.write_json()
Columns orient="columns" json.dumps(df.to_dict(as_series=False))
Index orient="index" json.dumps({i: row for i, row in enumerate(df.iter_rows(named=True))})
Values orient="values" json.dumps(df.to_numpy().tolist())
Sign up to request clarification or add additional context in comments.

Comments

1

To achieve a column-oriented JSON structure, You need to transform the DataFrame to a Dictionary before exporting Example:

import polars as pl
import json

df = pl.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
# json_data = df.write_json() # I guess you have tried this

# Convert to column-oriented JSON
# json_data = json.dumps(df.to_dict(as_series=False), indent=4)

# Convert to column-oriented JSON without newlines
json_data = json.dumps(df.to_dict(as_series=False), separators=(',', ':')) 
print(json_data)

Output: {"A": [1, 2, 3], "B": [4, 5, 6]}

2 Comments

Great, that did the trick! Thanks a lot! Just a minor detail, is there any way to avoid the newline chars in the resulting dict? (I know i can just trim them out in the resulting str)
I have edited my code check now. And also if you just remove indent=4 then you'll get JSON without newlines.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.