Convert some columns of objects to json string

Viewed 612

I want to convert some columns of a dataframe to json strings. I was hoping that something like the following would work but apply+json.dumps can't operate on a Series.

jcols = ['a','c']
df[jcols] = df[jcols].apply(json.dumps, axis=1).astype('string')

The following code DOES work but there must be a more straightforward way. The code uses list comprehension, Series, map(), dict to create a new dataframe containing the json string columns. What am I missing?

d = {'d':0}
df = pd.DataFrame({'a':[d,d,d],'b':[21,22,23],'c':[d,d,d]})
display(df,df.dtypes)
jcols = ['a','c']
jdf = pd.DataFrame({c:(df[c].map(json.dumps).astype('string') if c in jcols else df[c]) for c in df.columns})
display(jdf,jdf.dtypes)
2 Answers

Try with applymap instead:

df[jcols] = df[jcols].applymap(json.dumps).astype('string')

df:

          a   b         c
0  {"d": 0}  21  {"d": 0}
1  {"d": 0}  22  {"d": 0}
2  {"d": 0}  23  {"d": 0}

df.dtypes:

a    string
b     int64
c    string
dtype: object

Complete Working Example:

import pandas as pd

import json

d = {'d': 0}
df = pd.DataFrame({'a': [d, d, d], 'b': [21, 22, 23], 'c': [d, d, d]})
jcols = ['a', 'c']

df[jcols] = df[jcols].applymap(json.dumps).astype('string')
print(df)
print(df.dtypes)

I think it can be very simplistic:

>>> df = pd.DataFrame({'a':[d,d,d],'b':[21,22,23],'c':[d,d,d]})
>>> df
          a   b         c
0  {'d': 0}  21  {'d': 0}
1  {'d': 0}  22  {'d': 0}
2  {'d': 0}  23  {'d': 0}
>>> df.dtypes
a    object
b     int64
c    object

Simply without importing json it can be:

>>> df[jcols] = df[jcols].astype('string')
>>> df.dtypes
a    string
b     int64
c    string
dtype: object

Or even simpler if there are limited columns to change:

>>> df.astype({'a': 'string', 'c': 'string'}).dtypes
a    string
b     int64
c    string
dtype: object
Related