我正在与熊猫斗争,以获得以下的结果。请你帮我一下好吗?
这是一个代码:
response =[{'type': 'heartrate',
'data': [27, 32, 35, 31, 29, 30],
'education': 'bachelor',
'salary': 80000}].
df = pd.DataFrame(response)我得到了什么。
type data education salary
0 heartrate [27, 32, 35, 31, 29, 30] bachelor 80000我想达到的目标是:
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注意:我发现如果去掉方括号,它就能工作,但这实际上是api响应,因此不能修改原始数据。
发布于 2021-08-01 13:40:53
然后在data列上爆炸数据格式,ignore_index作为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发布于 2021-08-01 13:40:46
给你:
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)https://stackoverflow.com/questions/68610953
复制相似问题