简体   繁体   中英

Convert Python list to dataframe

I have a Python list like the following:

[{'date': '20200101',
  'Samsung': {'positive': 242, 'negative': 28, 'neutral': 69, 'etc': 98}},
 {'date': '20200102',
  'Samsung': {'positive': 270, 'negative': 45, 'neutral': 83, 'etc': 143}},
 {'date': '20200103',
  'Samsung': {'positive': 252, 'negative': 57, 'neutral': 90, 'etc': 113}},
 {'date': '20200104',
  'Samsung': {'positive': 292, 'negative': 57, 'neutral': 134, 'etc': 229}}]

I want to change like:

   target date       positive  negative neutral etc
0 Samsung 20200101   242       28       69      98
1 Samsung 20200102   270       45       33      143
2 Samsung 20200103   252       57       90      113
3 Samsung 20200104   292       74       134     229

Here is your data:

dd=[{'date': '20200101',
  'Samsung': {'positive': 242, 'negative': 28, 'neutral': 69, 'etc': 98}},
 {'date': '20200102',
  'Samsung': {'positive': 270, 'negative': 45, 'neutral': 83, 'etc': 143}},
 {'date': '20200103',
  'Samsung': {'positive': 252, 'negative': 57, 'neutral': 90, 'etc': 113}},
 {'date': '20200104',
  'Samsung': {'positive': 292, 'negative': 57, 'neutral': 134, 'etc': 229}}]


You need to change your dictionnary structure:

new_dict=[]
for record in dd:
  for k ,v in record.items():
    if k == "date":
      new_dict.append({k:v})
    else:
      new_dict[-1].update({"target":k})
      new_dict[-1].update(v)
new_dict

Which gives that

[{'date': '20200101',
  'etc': 98,
  'negative': 28,
  'neutral': 69,
  'positive': 242,
  'target': 'Samsung'},
 {'date': '20200102',
  'etc': 143,
  'negative': 45,
  'neutral': 83,
  'positive': 270,
  'target': 'Samsung'},
 {'date': '20200103',
  'etc': 113,
  'negative': 57,
  'neutral': 90,
  'positive': 252,
  'target': 'Samsung'},
 {'date': '20200104',
  'etc': 229,
  'negative': 57,
  'neutral': 134,
  'positive': 292,
  'target': 'Samsung'}]

And then pass it to pandas for a dataframe structure


df = pd.DataFrame(new_dict)
df

       date     target  positive    negative    neutral   etc
0   20200101    Samsung     242           28       69     98
1   20200102    Samsung     270           45       83     143
2   20200103    Samsung     252           57       90     113
3   20200104    Samsung     292           57       134    229

You can do the following (lst is your list):

import pandas as pd

df=pd.DataFrame(lst)

df2=pd.json_normalize(df['Samsung'])

df['target']=[list(i.keys())[1] for i in lst]

res=pd.concat([df[['target','date']], df2], axis=1)

print(res)

Output:

    target      date  positive  negative  neutral  etc
0  Samsung  20200101       242        28       69   98
1  Samsung  20200102       270        45       83  143
2  Samsung  20200103       252        57       90  113
3  Samsung  20200104       292        57      134  229

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