Pandas – how to reduce each row to a json column?

Question:

Let’s say we have the following df:

| col_a| col_b |
| -----| ----- |
| 1    | a     |
| 2    | b     |

And we want to reduce all rows to JSONs representing all columns row-wise:

|    json_representation        |
| ------------------------------| 
| {'col_a': 1, 'col_b': 'a'}    | 
| {'col_a': 2, 'col_b': 'b'}    |

Dicts are also good, since converting them to JSON strings is simple.

I am aiming for a solution where there is no need to know every column name, so answers here (up to the moment I am asking), are not the solution I am looking for.

How
Thanks.

Asked By: YFl

||

Answers:

here is one way to do it

use apply and convert the row using to_json

df.apply(lambda x: x.to_json( ), axis=1)


0    {"col_a":1,"col_b":"a"}
1    {"col_a":2,"col_b":"b"}
dtype: object
df['json']=df.apply(lambda x: x.to_json( ), axis=1)
df
col_a   col_b   json
0   1   a   {"col_a":1,"col_b":"a"}
1   2   b   {"col_a":2,"col_b":"b"}
Answered By: Naveed

try this:

your_df['all_json'] = your_df.apply(lambda i: {i.col_a: i.col_b}, axis=1)

Answered By: Clegane

try:

df
    col_a   col_b
0   1       a
1   2       b

#1. return dataframe
df1 = pd.DataFrame([str(i) for i in list(df.to_dict(orient="index").values())], columns=['json_representation'])

df1
    json_representation
0   {'col_a': 1, 'col_b': 'a'}
1   {'col_a': 2, 'col_b': 'b'}

#2. return series:
df2 = pd.Series([str(i) for i in list(df.to_dict(orient="index").values())])

df2
0    {'col_a': 1, 'col_b': 'a'}
1    {'col_a': 2, 'col_b': 'b'}

#3. return list of dictionaries (each row in a dict.):
d = list(df.to_dict(orient="index").values())

d
[{'col_a': 1, 'col_b': 'a'}, {'col_a': 2, 'col_b': 'b'}]


Answered By: khaled koubaa

You can do :

pd.read_fwf(StringIO(df.to_json(orient='records', lines=True)), header=None)
Answered By: SomeDude
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.