<jupyter_start><jupyter_text>Decode all JSON Columns<jupyter_code>df = pd.read_csv("tmdb_5000_movies.csv")

sample_type = type(df["genres"][0])
print(f"The data type before using apply function: {sample_type}\n")

# Column names that contain JSON
json_cols = ['genres', 'keywords', 'production_companies', 'spoken_languages', 'production_countries']


def clean_json(x):
    "Create apply function for decoding JSON"
    return json.loads(x)

# Apply the function column wise to each column of interest
for x in json_cols:
    df[x] = df[x].apply(clean_json)

sample_type2 = type(df["genres"][0])
print(f"The data type after using apply function: {sample_type2}")<jupyter_output>The data type before using apply function: <class 'str'>

The data type after using apply function: <class 'list'><jupyter_text>Access the Python data type like we normally would<jupyter_code># Look at first row of genres
df["genres"][0]
# Look at 2nd element in the list of dictionaries
df["genres"][0][1]
# Access "name" key to reveal associated value
df["genres"][0][1]["name"]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12213039485766758, 'fraction_numerical': 0.012855831037649219, 'mean_word_length': 6.517241379310345, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '456115', 'license': 'MIT', 'n_tokens_mistral': 373, 'n_tokens_neox': 362, 'n_words': 112, 'path': 'json/part2.ipynb', 'repo_name': 'mpHarm88/projects', 'size': '1089'}