从这里开始的系统方法是什么:
x = {'col0': [1, 1, 2, 2], 'col1': ['a', 'b', 'a', 'b'],
'col2': ['x', 'x', 'x', 'x'], 'col3': [12, 13, 14, 15]}
y = pd.DataFrame(data=x)
y
col0 col1 col2 col3
0 1 a x 12
1 1 b x 13
2 2 a x 14
3 2 b x 15
Run Code Online (Sandbox Code Playgroud)
对此:
y2
col0 col3__a_x col3__b_x
0 1 12 13
1 2 14 15
Run Code Online (Sandbox Code Playgroud)
我最初想到的是cast来自 R 的 reshape2 包。 然而,我对 Pandas/Python 的熟悉程度远不如对 R 的熟悉。
在我正在使用的数据集中col1有 3 …