How to explode Panda column with data having different dict and list of dict
Asked Answered
I

2

1

I have a panda dataframe with different set of values like first one is an list or array and other elements or not

>>> df_3['integration-outbound:IntegrationEntity.integrationEntityDetails.supplier.forms.form.records.record']
0    [{'Internalid': '24348', 'isDelete': 'false', 'fields': {'field': [{'id': 'CATEGOR_LEVEL_1', 'value': 'MR'}, {'id': 'LOW_PRODSERV', 'value': 'RES'}, {'id': 'LOW_LEVEL_2', 'value': 'keylevel221'}, {'id': 'LOW_LEVEL_3', 'value': 'keylevel3127'}, {'id': 'LOW_LEVEL_4', 'value': 'keylevel4434'}, {'id': 'LOW_LEVEL_5', 'value': 'keylevel5545'}]}}, {'Internalid': '24349', 'isDelete': 'false', 'fields': {'field': [{'id': 'CATEGOR_LEVEL_1', 'value': 'MR'}, {'id': 'LOW_PRODSERV', 'value': 'RES'}, {'id': 'LOW_LEVEL_2', 'value': 'keylevel221'}, {'id': 'LOW_LEVEL_3', 'value': 'keylevel3125'}, {'id': 'LOW_LEVEL_4', 'value': 'keylevel4268'}, {'id': 'LOW_LEVEL_5', 'value': 'keylevel5418'}]}}, {'Internalid': '24350', 'isDelete': 'false', 'fields': {'field': [{'id': 'CATEGOR_LEVEL_1', 'value': 'MR'}, {'id': 'LOW_PRODSERV', 'value': 'RES'}, {'id': 'LOW_LEVEL_2', 'value': 'keylevel221'}, {'id': 'LOW_LEVEL_3', 'value': 'keylevel3122'}, {'id': 'LOW_LEVEL_4', 'value': 'keylevel425'}, {'id': 'LOW_LEVEL_5', 'value': 'keylevel5221'}]}}]
0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      {'isDelete': 'false', 'fields': {'field': [{'id': 'S_EAST', 'value': 'N'}, {'id': 'W_EST', 'value': 'N'}, {'id': 'M_WEST', 'value': 'N'}, {'id': 'N_EAST', 'value': 'N'}, {'id': 'LOW_AREYOU_ASSET', 'value': '-1'}, {'id': 'LOW_SWART_PROG', 'value': '-1'}]}}
0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 {'isDelete': 'false', 'fields': {'field': {'id': 'LOW_COD_CONDUCT', 'value': '-1'}}}
0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     {'isDelete': 'false', 'fields': {'field': [{'id': 'LOW_SUPPLIER_TYPE', 'value': '2'}, {'id': 'LOW_DO_INT_BOTH', 'value': '1'}]}}

enter image description here

I want explode this into multiple rows. The first row is list and other rows or not ?

>>> type(df_3)
<class 'pandas.core.frame.DataFrame'>
>>> type(df_3['integration-outbound:IntegrationEntity.integrationEntityDetails.supplier.forms.form.records.record'])
<class 'pandas.core.series.Series'>

Expected output -

{'Internalid': '24348', 'isDelete': 'false', 'fields': {'field': [{'id': 'CATEGOR_LEVEL_1', 'value': 'MR'}, {'id': 'LOW_PRODSERV', 'value': 'RES'}, {'id': 'LOW_LEVEL_2', 'value': 'keylevel221'}, {'id': 'LOW_LEVEL_3', 'value': 'keylevel3127'}, {'id': 'LOW_LEVEL_4', 'value': 'keylevel4434'}, {'id': 'LOW_LEVEL_5', 'value': 'keylevel5545'}]}}
{'Internalid': '24349', 'isDelete': 'false', 'fields': {'field': [{'id': 'CATEGOR_LEVEL_1', 'value': 'MR'}, {'id': 'LOW_PRODSERV', 'value': 'RES'}, {'id': 'LOW_LEVEL_2', 'value': 'keylevel221'}, {'id': 'LOW_LEVEL_3', 'value': 'keylevel3125'}, {'id': 'LOW_LEVEL_4', 'value': 'keylevel4268'}, {'id': 'LOW_LEVEL_5', 'value': 'keylevel5418'}]}}
{'Internalid': '24350', 'isDelete': 'false', 'fields': {'field': [{'id': 'CATEGOR_LEVEL_1', 'value': 'MR'}, {'id': 'LOW_PRODSERV', 'value': 'RES'}, {'id': 'LOW_LEVEL_2', 'value': 'keylevel221'}, {'id': 'LOW_LEVEL_3', 'value': 'keylevel3122'}, {'id': 'LOW_LEVEL_4', 'value': 'keylevel425'}, {'id': 'LOW_LEVEL_5', 'value': 'keylevel5221'}]}}]
{'isDelete': 'false', 'fields': {'field': [{'id': 'S_EAST', 'value': 'N'}, {'id': 'W_EST', 'value': 'N'}, {'id': 'M_WEST', 'value': 'N'}, {'id': 'N_EAST', 'value': 'N'}, {'id': 'LOW_AREYOU_ASSET', 'value': '-1'}, {'id': 'LOW_SWART_PROG', 'value': '-1'}]}}
{'isDelete': 'false', 'fields': {'field': {'id': 'LOW_COD_CONDUCT', 'value': '-1'}}}
{'isDelete': 'false', 'fields': {'field': [{'id': 'LOW_SUPPLIER_TYPE', 'value': '2'}, {'id': 'LOW_DO_INT_BOTH', 'value': '1'}]}}

i tried to explode this columns

>>> df_3.explode('integration-outbound:IntegrationEntity.integrationEntityDetails.supplier.forms.form.records.record')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib64/python3.6/site-packages/pandas/core/frame.py", line 6318, in explode
    result = df[column].explode()
  File "/usr/local/lib64/python3.6/site-packages/pandas/core/series.py", line 3504, in explode
    values, counts = reshape.explode(np.asarray(self.array))
  File "pandas/_libs/reshape.pyx", line 129, in pandas._libs.reshape.explode
KeyError: 0

I can run through each row and try to find out if its a list and implement something but it doesnt seems right

if str(type(df_3.loc[i,'{}'.format(c)])) == "<class 'list'>":

Is there any way we ca use an explode function on such kind of data

Iphlgenia answered 9/7, 2020 at 23:5 Comment(0)
A
0

I was able to do it, but the exploded rows are all filtered to the top of the DataFrame (in case there are more list type object in lower rows).

pd.concat((df.iloc[[type(item) == list for item in df['Column']]].explode('Column'),
           df.iloc[[type(item) != list for item in df['Column']]]))

It essentially does what you've said: check if object type is list, if so, explode. Then concatenate this exploded Series with the rest of the data (i.e. the non-lists). Performance doesn't seem to hurt much from longer DataFrames.

Output:

Column
0   {'Internalid': '24348', 'isDelete': 'false', '...
0   {'Internalid': '24349', 'isDelete': 'false', '...
0   {'Internalid': '24350', 'isDelete': 'false', '...
1   {'isDelete': 'false', 'fields': {'field': [{'i...
2   {'isDelete': 'false', 'fields': {'field': {'id...
3   {'isDelete': 'false', 'fields': {'field': [{'i...
Aponte answered 10/7, 2020 at 0:43 Comment(0)
E
1

alternative way using pandas-read-xml

from pandas_read_xml import flatten, fully_flatten

df = flatten(df)
Ewald answered 1/9, 2020 at 20:0 Comment(0)
A
0

I was able to do it, but the exploded rows are all filtered to the top of the DataFrame (in case there are more list type object in lower rows).

pd.concat((df.iloc[[type(item) == list for item in df['Column']]].explode('Column'),
           df.iloc[[type(item) != list for item in df['Column']]]))

It essentially does what you've said: check if object type is list, if so, explode. Then concatenate this exploded Series with the rest of the data (i.e. the non-lists). Performance doesn't seem to hurt much from longer DataFrames.

Output:

Column
0   {'Internalid': '24348', 'isDelete': 'false', '...
0   {'Internalid': '24349', 'isDelete': 'false', '...
0   {'Internalid': '24350', 'isDelete': 'false', '...
1   {'isDelete': 'false', 'fields': {'field': [{'i...
2   {'isDelete': 'false', 'fields': {'field': {'id...
3   {'isDelete': 'false', 'fields': {'field': [{'i...
Aponte answered 10/7, 2020 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.