我有一个数据帧:转换大熊猫据帧要素元组
>>> df = pd.DataFrame(np.random.random((3,3)))
>>> df
0 1 2
0 0.732993 0.611314 0.485260
1 0.935140 0.153149 0.065653
2 0.392037 0.797568 0.662104
什么对我来说是转换每个条目为2元组,与第一个元素从目前的数据框最简单的方法,而第二个元素从最后一列('2'
)?
即我想最后的结果是:
0 1 2
0 (0.732993, 0.485260) (0.611314, 0.485260) (0.485260, 0.485260)
1 (0.935140, 0.065653) (0.153149, 0.065653) (0.065653, 0.065653)
2 (0.392037, 0.662104) (0.797568, 0.662104) (0.662104, 0.662104)
你为什么要这么做?更具体地说,为什么你要使用熊猫,如果你想保持数据的格式熊猫本身不支持?您最好将数据保留为当前格式,并将算法更改为从第二列明确处理数据。 –
例如,我想使用最后一列对所有其他列进行滚动回归。 AFAIK,这是不容易实现的([这里](https://stackoverflow.com/questions/44380068/pandas-rolling-regression-alternatives-to-looping),[here](https://stackoverflow.com/questions/21040766/python-pandas-rolling-apply-two-column-input-in-function),[here](https://stackoverflow.com/questions/19121854/using-rolling-apply-on-a-dataframe-对象)和[这里](https://stackoverflow.com/questions/21025821/python-custom-function-using-rolling-apply-for-pandas))。通过转换为元组,我有一个镜头。 – Zhang18