Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep column and row order when storing pandas dataframe in json

When storing data in a json object with to_json, and reading it back with read_json, rows and columns are returned sorted alphabetically. Is there a way to keep the results ordered or reorder them upon retrieval?

like image 557
Alon Avatar asked Apr 23 '17 14:04

Alon


People also ask

Does pandas DataFrame preserve order?

Pandas. DataFrame doesn't preserve the column order when converting from a DataFrames.

How do I save pandas DataFrame to JSON?

To convert the object to a JSON string, then use the Pandas DataFrame. to_json() function. Pandas to_json() is an inbuilt DataFrame function that converts the object to a JSON string. To export pandas DataFrame to a JSON file, then use the to_json() function.

How do I rearrange rows and columns in pandas?

Use the T attribute or the transpose() method to swap (= transpose) the rows and columns of pandas. DataFrame . Neither method changes the original object but returns a new object with the rows and columns swapped (= transposed object).

Does the order of columns matter in pandas?

No, it does not work for missing values. Then you start doing dropna or fillna on various columns that are not matching.

How to change the column order of a Dataframe in pandas?

Let’s get right into the different methods to change the column order of a dataframe in Pandas. This is one of the simplest methods to change the order of the columns of a pandas DataFrame object. In this method, we simply pass the Python list of columns of the DataFrame in the desired order to the DataFrame object.

How to retrieve rows from pandas Dataframe?

Pandas provide a unique method to retrieve rows from a Data frame. DataFrame.loc [] method is used to retrieve rows from Pandas DataFrame.

How bad is it to work with JSON data in pandas?

There you have it, working with JSON data in Pandas is not that bad and with a little creativity, you can write code to implement whatever your heart desires. Feel free to adjust the code so it doesn't save certain values or keys. The code provided is meant to serve as boilerplate for whatever JSON related problem you are dealing with.

How do I make a Dataframe only keep the team columns?

#drop columns 'col3' and 'col4' df [df.columns[~df.columns.isin( ['col3', 'col4'])]] The following code shows how to define a new DataFrame that only keeps the “team” and “points” columns: Notice that the resulting DataFrame only keeps the two columns that we specified.


1 Answers

You could use orient='split', which stores the index and column information in lists, which preserve order:

In [34]: df
Out[34]: 
   A  C  B
5  0  1  2
4  3  4  5
3  6  7  8

In [35]: df.to_json(orient='split')
Out[35]: '{"columns":["A","C","B"],"index":[5,4,3],"data":[[0,1,2],[3,4,5],[6,7,8]]}'

In [36]: pd.read_json(df.to_json(orient='split'), orient='split')
Out[36]: 
   A  C  B
5  0  1  2
4  3  4  5
3  6  7  8

Just remember to use orient='split' on reading as well, or you'll get

In [37]: pd.read_json(df.to_json(orient='split'))
Out[37]: 
  columns       data  index
0       A  [0, 1, 2]      5
1       C  [3, 4, 5]      4
2       B  [6, 7, 8]      3
like image 150
DSM Avatar answered Oct 21 '22 14:10

DSM