Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- data = [
- [123, [{'type': 'Name', 'value': 'A'}, {'type': 'Birthdate', 'value': '1991'}, {'type': 'Style', 'value': 'C'}]],
- [128, [{'type': 'Birthdate', 'value': '1994'}, {'type': 'Name', 'value': 'F'}, {'type': 'Colour', 'value': 'Z'}]],
- ]
- df = pd.DataFrame(data, columns=['pin', 'attributes'])
- print(df)
- # --- functions ---
- def convert(row):
- result = pd.Series(dtype=object)
- for item in row:
- key = item['type']
- val = item['value']
- result[key] = val
- return result
- # --- main ---
- new_columns = df['attributes'].apply(convert)
- df = df.join(new_columns)
- df = df.drop(columns=['attributes'])
- print(df)
- """
- Result:
- pin attributes
- 0 123 [{'type': 'Name', 'value': 'A'}, {'type': 'Bir...
- 1 128 [{'type': 'Birthdate', 'value': '1994'}, {'typ...
- pin Name Birthdate Style Colour
- 0 123 A 1991 C NaN
- 1 128 F 1994 NaN Z
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement