1

I am struggling with pandas to get the results per below. Can you pls help me on this?

Here is a code:

response =[{'type': 'heartrate', 
    'data': [27, 32, 35, 31, 29, 30],
    'education':  'bachelor',
    'salary': 80000}].     

df = pd.DataFrame(response)

What I got.

        type                      data education  salary
0  heartrate  [27, 32, 35, 31, 29, 30]  bachelor   80000

What I want to achieve:

  type  data education  salary
0  heartrate    27  bachelor   80000
1  heartrate    32  bachelor   80000
2  heartrate    35  bachelor   80000
3  heartrate    31  bachelor   80000
4  heartrate    29  bachelor   80000
5  heartrate    30  bachelor   80000

Note: I found out that it will work if I remove square brackets, but this is actually api response hence cannot modify the original data.

tat
  • 45
  • 4

2 Answers2

0

Here you go:

response ={'type': ['heartrate' for _ in range(6)], 
    'data': [27, 32, 35, 31, 29, 30],
    'education':  ['bachelor' for _ in range(6)],
    'salary': [80000 for _ in range(6)]}

df = pd.DataFrame(response)
sehan2
  • 1,700
  • 1
  • 10
  • 23
0

Then just explode the dataframe on data column, with ignore_index as True

>>> df.explode('data', ignore_index=True)

        type data education  salary
0  heartrate   27  bachelor   80000
1  heartrate   32  bachelor   80000
2  heartrate   35  bachelor   80000
3  heartrate   31  bachelor   80000
4  heartrate   29  bachelor   80000
5  heartrate   30  bachelor   80000
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45