1 回答
TA贡献1887条经验 获得超5个赞
尝试这个:
df_out = (df.set_index(['Node', df.groupby('Node').cumcount()])[['dx', 'dy', 'dz']]
.unstack().sort_index(level=1, axis=1))
df_out.columns = [f'{i}{j}' for i, j in df_out.columns]
df_out
输出:
dx0 dy0 dz0 dx1 dy1 dz1 dx2 dy2 dz2
Node
1 3.385000e-07 0.0 1.636000e-07 0.0 0.0 0.0 -1.334000e-07 -2.684000e-07 -0.000022
5 1.926000e-07 0.0 -2.247000e-08 0.0 0.0 0.0 2.315000e-07 -2.684000e-07 -0.00003
将 cumcount 加 + 1:
df_out = (df.set_index(['Node', df.groupby('Node').cumcount() + 1])[['dx', 'dy', 'dz']]
.unstack().sort_index(level=1, axis=1))
df_out.columns = [f'{i}{j}' for i, j in df_out.columns]
df_out
输出:
dx1 dy1 dz1 dx2 dy2 dz2 dx3 dy3 dz3
Node
1 3.385000e-07 0.0 1.636000e-07 0.0 0.0 0.0 -1.334000e-07 -2.684000e-07 -0.000022
5 1.926000e-07 0.0 -2.247000e-08 0.0 0.0 0.0 2.315000e-07 -2.684000e-07 -0.000036
添加回答
举报