我有一个包含多个包含多个值列表的单元格的数据框。我想扩展数据框架,而不是在一个单元格中存储多个值,这样列表中的每个项目都会获得自己的行,而其他所有列中的值都相同。我有示例代码和输出附件,还有我要实现的目标
import pandas as pd
import numpy as np
df=pd.DataFrame({'OBJECTID':[1,2,3,4,5],
'CRASH_KEY':[20191088662,20191088664,20191088668,20191088677,20191088678],
'CASENUMBER':[20191088662,20191088664,20191088668,20191088677,20191088679],
'routeId':[list(np.random.randn(3).round(2)) for i in range(5)],
'measure':[list(np.random.randn(3).round(2)) for i in range(5)],
'SPEED_LIMIT':[list(np.random.randn(3).round(2)) for i in range(5)],
'distance':[list(np.random.randn(3).round(2)) for i in range(5)],})
df 我的输出
OBJECTID CRASH_KEY CASENUMBER ... measure SPEED_LIMIT distance
0 1 20191088662 20191088662 ... [-0.24, -0.78, -0.7] [-1.28, 0.11, -0.7] [-0.98, 0.06, -1.07]
1 2 20191088664 20191088664 ... [-0.94, -0.98, 1.31] [-1.26, 1.31, -2.24] [-1.21, -1.21, -0.35]
2 3 20191088668 20191088668 ... [-0.21, -0.99, -0.25] [-1.36, -0.59, 0.02] [0.22, -1.36, -0.64]
3 4 20191088677 20191088677 ... [0.31, -0.56, 0.95] [-0.92, -1.04, 0.59] [1.88, 0.3, 0.95]
4 5 20191088678 20191088679 ... [-0.07, 1.02, 2.14] [-0.49, -0.66, 0.07] [-0.27, -0.12, 0.36]
这就是我要实现的目标
OBJECTID CRASH_KEY CASENUMBER measure SPEED_LIMIT distance
0 1 20191088662 20191088662 -0.24 -1.28 -0.98
1 1 20191088662 20191088662 -0.78 0.11 0.06
2 1 20191088662 20191088662 -0.7 -0.7 -1.07