简体   繁体   中英

Explode a column in dataframe in python (pandas)

I'm trying to explode a column in python. Tried several options how to do so but nothing worked.

Input dataframe:

Column_1 Column_2 Column_3 Column_4 Column_5 ... Column_N
text     text     text     {'address1': 'text', 'address2': None, 'address3': '', 'city': 'text', 'zip_code': 'text', 'country': 'text', 'state': 'text', 'display_address': [text]}
text     text     text     {'address1': 'text', 'address2': None, 'address3': '', 'city': 'text', 'zip_code': 'text', 'country': 'text', 'state': 'text', 'display_address': [text]}
text     text     text     {'address1': 'text', 'address2': None, 'address3': '', 'city': 'text', 'zip_code': 'text', 'country': 'text', 'state': 'text', 'display_address': [text]}
text     text     text     {'address1': 'text', 'address2': None, 'address3': '', 'city': 'text', 'zip_code': 'text', 'country': 'text', 'state': 'text', 'display_address': [text]}

I need to keep every column but to explode the column Column_4 with columns: address1, address2 and so on with the values from the dict.

Column_1 Column_2 Column_3 address1 address2 address3 city zip_code ...Column_N
text     text     text     text     text     text     text text
text     text     text     text     text     text     text text
text     text     text     text     text     text     text text
text     text     text     text     text     text     text text

So, I need to keep every column from the input dataframe but to drop Column_4 for columns from the dicts.

import ast
from io import StringIO
# sample data
s = """Column_1|Column_2|Column_3|Column_4
text|text|text|{'address1': 'text', 'address2': None, 'address3': '', 'city': 'text', 'zip_code': 'text', 'country': 'text', 'state': 'text', 'display_address': ['text']}
text|text|text|{'address1': 'text', 'address2': None, 'address3': '', 'city': 'text', 'zip_code': 'text', 'country': 'text', 'state': 'text', 'display_address': ['text']}
text|text|text|{'address1': 'text', 'address2': None, 'address3': '', 'city': 'text', 'zip_code': 'text', 'country': 'text', 'state': 'text', 'display_address': ['text']}
text|text|text|{'address1': 'text', 'address2': None, 'address3': '', 'city': 'text', 'zip_code': 'text', 'country': 'text', 'state': 'text', 'display_address': ['text']}"""
df = pd.read_csv(StringIO(s), sep='|')
df['Column_4'] = df['Column_4'].apply(ast.literal_eval)
# end sample data

# list comprehension and concat dataframe
con = pd.concat([pd.DataFrame(x) for x in df['Column_4'].values.tolist()]).reset_index(drop=True)
# concat list of dataframe with the original df
new_df = pd.concat([df,con], axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM