将一列的值转换为标题,同时保留所有其他列

时间:2020-08-26 10:27:33

标签: python pandas

我在Pandas中有一个数据框,如下所示:

const onScroll = (event) => {
   const { y } = event.nativeEvent.contentOffset;
   const { height: layoutHeight } = event.nativeEvent.layoutMeasurement;
   const value = Math.floor(y / itemHeight) + Math.floor(layoutHeight / itemHeight / 2);

   if (activeItem !== value) {
      setActiveItem(value);
   }
   }

我想将主题列转换为列标题,同时保留所有其他列,如下所示:

+--------+------------+---------+-------+
|class   | student_id | subject | score |
+--------+------------+---------+-------+
|4       |           5| Maths   |     56|
|4       |           5| English |     65|
|4       |           6| Maths   |     73|
|4       |           6| English |     78|
+--------+------------+---------+-------+

我不需要任何聚合,只需在特定列上转置即可。

我一直在尝试以各种方式使用数据透视,但是我无法实现此结果。过去,我可以使用R中+--------+------------+---------+-------+ |class | student_id | English | Maths | +--------+------------+---------+-------+ |4 | 5| 65| 56| |4 | 6| 78| 73| +--------+------------+---------+-------+ 库的gather命令很容易地做到这一点。

我如何在熊猫中做到这一点?

0 个答案:

没有答案