<jupyter_start><jupyter_text>---title: "Expand Cells Containing Lists Into Their Own Variables In Pandas"author: "<NAME>"date: 2017-12-20T11:53:49-07:00description: "Expand cells containing lists into their own variables in pandas."type: technical_notedraft: false---<jupyter_code># import pandas
import pandas as pd
# create a dataset
raw_data = {'score': [1,2,3], 
        'tags': [['apple','pear','guava'],['truck','car','plane'],['cat','dog','mouse']]}
df = pd.DataFrame(raw_data, columns = ['score', 'tags'])

# view the dataset
df
# expand df.tags into its own dataframe
tags = df['tags'].apply(pd.Series)

# rename each variable is tags
tags = tags.rename(columns = lambda x : 'tag_' + str(x))

# view the tags dataframe
tags
# join the tags dataframe back to the original dataframe
pd.concat([df[:], tags[:]], axis=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16374269005847952, 'fraction_numerical': 0.025730994152046785, 'mean_word_length': 5.7936507936507935, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '162393', 'license': 'CC0-1.0', 'n_tokens_mistral': 298, 'n_tokens_neox': 277, 'n_words': 84, 'path': 'content/python/data_wrangling/pandas_expand_cells_containing_lists.ipynb', 'repo_name': 'vedraiyani/notes-1', 'size': '860'}