Home > Net >  Expand dict to dataframe with one value of key is list
Expand dict to dataframe with one value of key is list

Time:11-03

I am new to python and try to figure out this one, can't find this anywhere please help!

I have a dictionary like the following: dict1 = {'id':1, 'col': 'b', 'item': [{'num': 111, 'value': 222},{'num': 333, 'value': 444}]}

I know how to expand first list in 'item' to dataframe into one row, my code are as follow:

df = pd.DataFrame()
df = df.append(dict1, ignore_index=True)
item_detail = df.loc[0, 'item'][0]
for k, v in item_detail.items():
    df.loc[0, str("item_"   k)] = v

note that I have to turn dict1 to df first because dict1 is from larger dict and the result will be

    id  col item                                                  item_num item_value
0   1   b   [{'num': 111, 'value': 222}, {'num': 333, 'val...   111.0   222.0

but I want

    id  col item                                                  item_num item_value
0   1   b   [{'num': 111, 'value': 222}, {'num': 333, 'val...   111.0   222.0
1   1   b   [{'num': 111, 'value': 222}, {'num': 333, 'val...   333.0   444.0

and I cant simply write df.locp[1, tr("item_" k)] = v because my loc i is written as range and I have multiple dict.

anyway I can turn the dict in same row into two or multiple row to dataframe?

please let me know if my question is not clear or need any additional info! thank you guys so much!

I had already tried to find answer and I am fresh to python. Thanks

UPDATE: I have no idea how many dict will be in the list 'item', if 'item' contain 4 {} I would like to separate into 4 row with same 'id' and 'col'.

CodePudding user response:

In fact, the solution is to iterate on the item list and append each time the same row with id, col and the new num and value

df = pd.DataFrame()
df = df.append({'id':1, 'col': 'b', 'item': [{'num': 111, 'value': 222},{'num': 333, 'value': 444}]}, ignore_index=True)
item_detail = df.loc[0, 'item']
for item in item_detail:
    new_row = df.iloc[0].copy()
    new_row["item_num"] = item["num"]
    new_row["item_value"] = item["value"]
    df = df.append(new_row, ignore_index=True).dropna()

output:

id  col item                                                item_num item_value
0   1   b   [{'num': 111, 'value': 222}, {'num': 333, 'val...   111.0   222.0
1   1   b   [{'num': 111, 'value': 222}, {'num': 333, 'val...   333.0   444.0
  • Related