这是我拥有的Pandas Dataframe:
Trial Time Condition Status Q Operation
ATAB 09:57:09 HEAT-1255 Jump_starting 34 New
ATAB 09:57:09 HEAT-7319 Farenheit_Rise 0 New
ATAB 09:57:09 HEAT-5005 Surface_Celcius 0 New
ATAB 09:57:09 HEAT-7891 Pressure_Drop 2 Rob_Qn_1
ATAB 09:57:09 HEAT-1000 Surface_Pressure 0 On_Now
ATAB 09:58:18 HEAT-1255 Jump_starting 35 New
ATAB 09:58:18 HEAT-7319 Farenheit_Rise 0 New
ATAB 09:58:18 HEAT-5005 Surface_Celcius 0 New
ATAB 09:58:18 HEAT-7891 Pressure_Drop 2 Rob_Qn_1
ATAB 09:58:18 HEAT-1000 Surface_Pressure 0 On_Now
ATAB 09:59:16 HEAT-1255 Jump_starting 35 New
ATAB 09:59:16 HEAT-7319 Farenheit_Rise 0 New
ATAB 09:59:16 HEAT-5005 Surface_Celcius 0 New
ATAB 09:59:16 HEAT-7759 Import_Eraser 8 Runing_now
ATAB 09:59:16 HEAT-7891 Pressure_Drop 2 Rob_Qn_1
ATAB 09:59:16 HEAT-1000 Surface_Pressure 1 Alb_77
ATAC 01:29:44 HEAT-5005 Surface_Celcius 0 New
ATAC 01:29:44 HEAT-7759 Import_Eraser 8 Runing_now
ATAC 01:29:44 HEAT-7891 Pressure_Drop 2 Rob_Qn_1
ATAC 01:29:44 HEAT-1000 Surface_Pressure 1 Alb_77
ATAD 08:49:30 HEAT-7319 Farenheit_Rise 0 New
ATAD 08:49:30 HEAT-5005 Surface_Celcius 0 New
ATAD 08:49:30 HEAT-7759 Import_Eraser 8 Runing_now
ATAD 08:49:30 HEAT-7891 Pressure_Drop 2 Rob_Qn_1
ATAD 08:49:30 HEAT-1000 Surface_Pressure 1 Alb_77
在给定时间内,它列出了各种条件,状态和正在执行的操作。各种状态和操作 每次都存在。
我正在尝试以这样一种方式旋转Dataframe,即将Status列的行放在1个标题行上。然后,每次,我需要把 他们在每个标题下的相应操作。这就是我要找的东西:
Trial Time Jump_starting Farenheit_Rise Surface_Celcius Pressure_Drop Surface_Pressure Import_Eraser
ATAB 09:57:09 34 0 0 2 0 N/A
....
....
....
基本上,我需要反转Operation
列,同时保持试用,时间和条件行不变。
如何在Python 2.7中使用Pandas执行此操作?
编辑: 要创建Dataframe,请将此帖子中的数据框复制到剪贴板,然后使用以下行:
import pandas as pd
df = pd.read_clipboard()
答案 0 :(得分:1)
我原以为你可以使用pivot(),但是看起来你只能指定一个列作为索引。所以我认为使用unstack()是这里的方法。首先,我们需要设置索引=试用,时间和状态
df[["Trial","Time","Status","Q"]].set_index(["Trial","Time","Status"]).unstack("Status")
给你
Q
Status Farenheit_Rise Import_Eraser Jump_starting Pressure_Drop Surface_Celcius Surface_Pressure
Trial Time
ATAB 09:57:09 0 NaN 34 etc
09:58:18 0 NaN 35 ...
09:59:16 0 8 35 ...
ATAC 01:29:44 NaN 8 NaN ...
ATAD 08:49:30 0 8 NaN ...
编辑:得到一些澄清
在条件中包含条件,您只需执行此操作
df[["Trial","Time","Condition","Status","Q"]].set_index(
["Trial","Time","Condition","Status"]).unstack("Status")