There are many possible solutions. Generally though, you'll probably want to:

  1. Not loop over fields; instead let Pandas split the fields for you
  2. Use an actual missing value
    • But later if you want to represent it differently, you can do that, e.g. using the na_rep parameter to df.style.format

For the first step, you can look at Split / Explode a column of dictionaries into separate columns with pandas. I'll use Lech Birek's solution (json_normalize) then drop the "id" columns and rename the "value" columns.

headers_mapping = {'1': 'field1', '2': 'field2', '3': 'field3', '4': 'field4'}
(
    pd.json_normalize(df['json_field'])
    .filter(like='value')
    .rename(columns=lambda label: headers_mapping[label.rstrip('.value')])
)
   field1  field2  field3  field4
0  value1  value2     NaN     NaN
1  value1     NaN  value3     NaN
2     NaN     NaN  value3  value4

If you also need to sort the columns, tack this on at the end:

.reindex(columns=headers_mapping.values())
Answer from wjandrea on Stack Overflow
Discussions

python 3.x - Splitting a pandas data frame's column containing json data into multiple columns - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
March 4, 2019
python - Pandas: Convert a JSON column with multiple rows into multiple dataframe rows - Stack Overflow
I have a dataframe with two columns: countries and year. The countries column is JSON in the form of: [{'continent': 'europe', 'country': 'Yugoslavia', 'income': None, 'life_exp': None, ' More on stackoverflow.com
🌐 stackoverflow.com
python 3.x - How to split a JSON single column into Pandas DataFrame multiple columns? - Stack Overflow
I have a JSON file which has all data in one single column. I tried importing using JSON library with URLLIB3, Pandas, and so on, but with some difficulties. I am not sure if the issue is with the ... More on stackoverflow.com
🌐 stackoverflow.com
February 21, 2023
python - Pandas: Splitting JSON list value into new columns - Stack Overflow
I used Pandas to load a CSV to the following DataFrame: value values 0 56.0 [-0.5554548,10.0748005,4.23... More on stackoverflow.com
🌐 stackoverflow.com
March 5, 2018
🌐
Stack Overflow
stackoverflow.com › questions › 64916148 › how-to-split-a-json-string-column-in-pandas-spark-dataframe
python - How to split a json string column in pandas/spark dataframe? - Stack Overflow
import pandas as pd import json raw_data = [{'id': 1, 'name': 'NATALIE', 'json_result': '{"0": {"_source": {"person_id": 101, "firstname": "NATALIE", "lastname": "OSHO", "city_name": "WESTON"}}}'}, \ {'id': 2, 'name': 'MARK', 'json_result': '{"0": {"_source": {"person_id": 102, "firstname": "MARK", "lastname": "BROWN", "city_name": "NEW YORK"}}}'}, \ {'id': 3, 'name': 'NANCY', 'json_result': '{"0": {"_source": {"person_id": 103, "firstname": "NANCY", "lastname": "GATES", "city_name": "LA"}}}'}] df = pd.DataFrame.from_dict(raw_data) ser = df['json_result'].apply(lambda s: pd.json_normalize(json.loads(s))) a = df.drop(columns=['json_result']) b = pd.concat(list(ser), ignore_index=True) c = a.join(b) import sys c.to_csv(sys.stdout, index=False)
🌐
Stack Overflow
stackoverflow.com › questions › 54971005 › splitting-a-pandas-data-frames-column-containing-json-data-into-multiple-column
python 3.x - Splitting a pandas data frame's column containing json data into multiple columns - Stack Overflow
March 4, 2019 - I loaded and normalized a json data as: json_string = json.loads(data) df_norm = json_normalize(json_string, errors='ignore') Say it has now 2 columns: Group Members A [{'id':'1', '
Top answer
1 of 4
1

should add ignore_index=True argument in explode function to make sure the following join is not messed up.

df = pd.DataFrame(data).explode('countries', ignore_index=True)
df = df.join(pd.json_normalize(df.pop('countries')))
print(df)
2 of 4
0

You could try this with explode:

df=df.explode('countries')
#we add to each dictionary the respective value of year with key 'year'
df['countries']=[{**dc,**{'year':y}} for dc,y in zip(df['countries'],df['year'])]
pd.DataFrame(df['countries'].tolist())

Example:

j = [{'continent': 'europe',
 'country': 'Yugoslavia',
 'income': None,
  'life_exp': None,
'population': 4687422},
{'continent': 'asia',
'country': 'United Korea (former)',
'income': None,
'life_exp': None,
'population': 13740000}]
df=pd.DataFrame({'countries':[j,j],'year':[1800,1900]})
print(df)

df=df.explode('countries')
print(df)

#Here we add the key 'year' with the respective year row value to each dictionary
df['countries']=[{**dc,**{'year':y}} for dc,y in zip(df['countries'],df['year'])]
print(df['countries'])

finaldf=pd.DataFrame(df['countries'].tolist())
print(finaldf)

Output:

original df:
                                           countries  year
0  [{'continent': 'europe', 'country': 'Yugoslavi...  1800
1  [{'continent': 'europe', 'country': 'Yugoslavi...  1900


    

df(after explode): 
                                                                                            
                                           countries  year
0  {'continent': 'europe', 'country': 'Yugoslavia...  1800
0  {'continent': 'asia', 'country': 'United Korea...  1800
1  {'continent': 'europe', 'country': 'Yugoslavia...  1900
1  {'continent': 'asia', 'country': 'United Korea...  1900


df.countries(with year added):
0    {'continent': 'europe', 'country': 'Yugoslavia', 'income': None, 'life_exp': None, 'population': 4687422, 'year': 1800}
0    {'continent': 'asia', 'country': 'United Korea (former)', 'income': None, 'life_exp': None, 'population': 13740000, 'year': 1800}
1    {'continent': 'europe', 'country': 'Yugoslavia', 'income': None, 'life_exp': None, 'population': 4687422, 'year': 1900}
1    {'continent': 'asia', 'country': 'United Korea (former)', 'income': None, 'life_exp': None, 'population': 13740000, 'year': 1900}
Name: countries, dtype: object

finaldf
  continent                country income life_exp  population  year
0    europe             Yugoslavia   None     None     4687422  1800
1      asia  United Korea (former)   None     None    13740000  1800
2    europe             Yugoslavia   None     None     4687422  1900
3      asia  United Korea (former)   None     None    13740000  1900
🌐
105local
105local.com › 5xul9 › pandas-split-json-column-into-multiple-columns
pandas split json column into multiple columns
Ionic 2 - how to make ion-button with icon and text on two lines? Method #1 : Using Series.str.split () functions. Pandas provide a method to split string around a passed separator/delimiter. Split 'Number' column into two individual columns : 0 1 0 +44 3844556210 1 +44 2245551219 2 +44 1049956215 .
Find elsewhere
Top answer
1 of 3
1

I can use rdd to get the columns, data and create dataframe with this.

rdd = sc.textFile('test.txt')

import json
cols = rdd.map(lambda x: json.loads(x)['columns']).take(1)[0]
data = rdd.map(lambda x: json.loads(x)['data']).take(1)[0]

df = spark.createDataFrame(data, cols)
df.show(truncate=False)

+--------------+-----------+--------------+---------------------+------------+-----------------+-------------------------------+-----------+--------------+---------+------------+----------------------+------------+-------------+---------------------------+------------------+----------------------+-------------+----------------------------+-------------------------------+-------------------------+-----------+------------------+-------------------+--------------------------------+-----------------------+---------------------------+------------------------------------+------------------+-------------------+------------------------------------+---------------------------------+------------------------------+----------------+--------------+
|ApplicationNum|eads59Us01S|HouseDeal_flag|Liability_Asset_Ratio|CBRAvailPcnt|CMSFairIsaacScore|OweTaxes_or_IRAWithdrawalHistry|eads14Fi02S|GuarantorCount|CBRRevMon|CBRInstalMon|CMSApprovedToRequested|SecIncSource|eads59Us01S_4|Liability_Asset_Ratio_40_90|CBRAvailPcnt_20_95|CMSFairIsaacScore_Fund|eads14Fi02S_2|InstalMonthlyPayments_400_3k|RevolvingMonthlyPayments_1k_cap|ApprovedToRequested_0_100|NoSecIncome|coef_eads59Us01S_4|coef_HouseDeal_flag|coef_Liability_Asset_Ratio_40_90|coef_CBRAvailPcnt_20_95|coef_CMSFairIsaacScore_Fund|coef_OweTaxes_or_IRAWithdrawalHistry|coef_eads14Fi02S_2|coef_GuarantorCount|coef_RevolvingMonthlyPayments_1k_cap|coef_InstalMonthlyPayments_400_3k|coef_ApprovedToRequested_0_100|coef_NoSecIncome|coef_Intercept|
+--------------+-----------+--------------+---------------------+------------+-----------------+-------------------------------+-----------+--------------+---------+------------+----------------------+------------+-------------+---------------------------+------------------+----------------------+-------------+----------------------------+-------------------------------+-------------------------+-----------+------------------+-------------------+--------------------------------+-----------------------+---------------------------+------------------------------------+------------------+-------------------+------------------------------------+---------------------------------+------------------------------+----------------+--------------+
|569325.0      |2          |0.0           |1                    |92          |825              |0.0                            |4          |1.0           |74       |854         |0.51                  |2           |2.0          |0.9                        |92.0              |825.0                 |4.0          |854.0                       |1000.0                         |0.51                     |0.0        |0.11716245        |0.299528064        |0.392119645                     |-0.010826643           |-0.004957868               |0.339407077                         |0.061509795       |0.3685047          |1.67603E-4                          |2.25742E-4                       |0.902205454                   |-0.371734864    |2.788087559   |
+--------------+-----------+--------------+---------------------+------------+-----------------+-------------------------------+-----------+--------------+---------+------------+----------------------+------------+-------------+---------------------------+------------------+----------------------+-------------+----------------------------+-------------------------------+-------------------------+-----------+------------------+-------------------+--------------------------------+-----------------------+---------------------------+------------------------------------+------------------+-------------------+------------------------------------+---------------------------------+------------------------------+----------------+--------------+
2 of 3
1

You can use the function json.loads, transform the json string into a dictionary with column-data-pairs and create new columns from this dictionary with .apply(pd.Series)

import json
import pandas as pd

df = pd.DataFrame([["""{"columns":["ApplicationNum","eads59Us01S","HouseDeal_flag","Liability_Asset_Ratio","CBRAvailPcnt","CMSFairIsaacScore","OweTaxes_or_IRAWithdrawalHistry","eads14Fi02S","GuarantorCount","CBRRevMon","CBRInstalMon","CMSApprovedToRequested","SecIncSource","eads59Us01S_4","Liability_Asset_Ratio_40_90","CBRAvailPcnt_20_95","CMSFairIsaacScore_Fund","eads14Fi02S_2","InstalMonthlyPayments_400_3k","RevolvingMonthlyPayments_1k_cap","ApprovedToRequested_0_100","NoSecIncome","coef_eads59Us01S_4","coef_HouseDeal_flag","coef_Liability_Asset_Ratio_40_90","coef_CBRAvailPcnt_20_95","coef_CMSFairIsaacScore_Fund","coef_OweTaxes_or_IRAWithdrawalHistry","coef_eads14Fi02S_2","coef_GuarantorCount","coef_RevolvingMonthlyPayments_1k_cap","coef_InstalMonthlyPayments_400_3k","coef_ApprovedToRequested_0_100","coef_NoSecIncome","coef_Intercept"],"data":[[569325.0,2,0.0,1,92,825,0.0,4,1.0,74,854,0.51,2,2.0,0.9,92.0,825.0,4.0,854.0,1000.0,0.51,0.0,0.11716245,0.299528064,0.392119645,-0.010826643,-0.004957868,0.339407077,0.061509795,0.3685047,0.000167603,0.000225742,0.902205454,-0.371734864,2.788087559]]}"""]], columns=['json_string'])
df['json_loads'] = df['json_string'].apply(json.loads)
df['column_names'] = df['json_loads'].apply(lambda x: x['columns'])
df['data'] = df['json_loads'].apply(lambda x: x['data'][0])

# turning it into a dictionary
df['dict_values']=df.apply(lambda x: dict(zip(x['column_names'],x['data'])), axis=1)

df = pd.concat([df, df['dict_values'].apply(pd.Series)], axis=1)

print(df.head())
🌐
Stack Overflow
stackoverflow.com › questions › 73420669 › how-to-extract-database-column-in-json-format-into-multiple-columns-in-dataframe
python - how to extract database column in json format into multiple columns in dataframe - Stack Overflow
I have a database column that's been converted to a Pandas dataframe and it looks like below . My actual data has much more columns and rows with different key: value pair. df["Records"] {"ID":"1","ID_1":"40309","type":"type1"} {"ID":"2","ID_1":"40310","type":"type1"} {"ID":"3","ID_1":"40311","type":"type1"} I want to split this into multiple columns in a dataframe. df1: ID ID_1 type 1 40309 type1 2 40310 type1 3 40311 type1 ... json_Str=df.to_dict() json_dump= json.dumps(json_Str) json_dump=json_dump.replace("\\", "") with open("H:\\df2.json", 'w') as fp: # json.dump(result, fp, indent=4) print(json_dump, file=fp)
🌐
Stack Overflow
stackoverflow.com › questions › 57518356 › how-to-split-json-data-in-columns-to-multi-columns-like-this
python - How to split json data in columns to multi columns like this - Stack Overflow
August 16, 2019 - In [23]: for k, v in js.items(): ...: for r, i in v.items(): ...: for c in range(len(i)): ...: new_column = "{}({})".format(k, c + 1) ...: print(new_column) ...: flate[new_column].append(i[c]) ... In [25]: df2 = pd.DataFrame(data=flate) In [26]: df2 Out[26]: colA(1) colA(2) colA(3) colB(1) colB(2) colB(3) 0 3 6 3 6 5 8 1 1 2 3 9 9 8 ... Sign up to request clarification or add additional context in comments. ... import pandas as pd dfs = [] for c in df: tmp = pd.DataFrame(list(df[c])) tmp.columns = [c + '(%s)' % str(i+1) for i in range(tmp.shape[1])] dfs.append(tmp) new_df = pd.concat(dfs, 1) print(new_df)
🌐
YouTube
youtube.com › watch
How to Split JSON String Column in a Pandas DataFrame into Multiple Columns - YouTube
March 17, 2025 - Discover how to effectively `split a JSON string column` in a Pandas DataFrame into multiple columns using Python. Free up your data processes with this simp...
Top answer
1 of 2
1

I hope I've understood your question well. Try:

from ast import literal_eval

df["experimental_properties"] = df["experimental_properties"].apply(
    lambda x: {d["name"]: d["property"] for d in literal_eval(x)}
)
df = pd.concat([df, df.pop("experimental_properties").apply(pd.Series)], axis=1)

print(df)

Prints:

            Boiling Point                                Density
0                115.3 °C                                    NaN
1  91 °C @ Press: 20 Torr                                    NaN
2  58 °C @ Press: 12 Torr  0.8753 g/cm<sup>3</sup> @ Temp: 20 °C
2 of 2
0

Is the expected output really what you are looking for? Another way to visualise the data would be to have "name", "property", and "sourceNumber" as column names.

import json
import pandas as pd

data = [
'''[{'name': 'Boiling Point', 'property': '115.3 °C', 'sourceNumber': 1}]''',
'''[{'name': 'Boiling Point', 'property': '91 °C @ Press: 20 Torr', 'sourceNumber': 1}]''',
'''[{'name': 'Boiling Point', 'property': '58 °C @ Press: 12 Torr', 'sourceNumber': 1}, {'name': 'Density', 'property': '0.8753 g/cm<sup>3</sup> @ Temp: 20 °C', 'sourceNumber': 1}]''']

#Initialise a naiveList
naiveList = []

#String to List
for i in data:
    tempStringOfData = i
    tempStringOfData = tempStringOfData.replace("\'", "\"")
    tempJsonData = json.loads(tempStringOfData)
    naiveList.append(tempJsonData)

#Initialise a List for Dictionaries
newListOfDictionaries = []
for i in naiveList:
    for j in i:
        newListOfDictionaries.append(j)

df = pd.DataFrame(newListOfDictionaries)
print(df)

Which gives you

            name                               property  sourceNumber
0  Boiling Point                               115.3 °C             1
1  Boiling Point                 91 °C @ Press: 20 Torr             1
2  Boiling Point                 58 °C @ Press: 12 Torr             1
3        Density  0.8753 g/cm<sup>3</sup> @ Temp: 20 °C             1
🌐
Stack Overflow
stackoverflow.com › questions › 52379364 › how-to-split-dataframe-column-containing-json-array
python 3.x - How to split dataframe column containing json array? - Stack Overflow
September 18, 2018 - and split generated column into multiple columns. json · python-3.x · pandas · dataframe · split · Share · Improve this question · Follow · asked Sep 18, 2018 at 5:13 · Rohan Pawar · 13111 silver badge1313 bronze badges 4 · 3 · What is reason not use json_normalize(data1,'languages') what working very nice?