本文介绍了Python-用实际值替换分数(转置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据

 Time 	          Pressure	Normal/Abnormal
11/30/2011 22:50	74.3	  0
11/30/2011 23:00	74.8	  1
11/30/2011 23:10	77.7	  1
11/30/2011 23:30	74.8	  0
11/30/2011 13:00	80.9	  0
Desired Output:

Time 	           Normal	Time 	           Abnormal
11/30/2011 22:50	74.3	11/30/2011 23:00	74.8
11/30/2011 23:30	74.8	11/30/2011 23:10	77.7
11/30/2011 13:00	80.9		

我想转置所需输出"中提到的行.我知道我需要使用类似于melt和cast(在R中使用)的东西,但我不确定如何使用它们.

I want to transpose the rows like mentioned in the "desired output".I understand that I need to use something similar to melt and cast(used in R),but am unsure how to use them.

推荐答案

构造两个数据框,1个为普通&1 为异常然后 concat &编辑列名

construct two data frames, 1 for normal & 1 for abnormal and then concat & edit column names

out = pd.concat([
  df[df['Normal/Abnormal'] == k].iloc[:, [0,1]].reset_index(drop=True)
  for k in [0, 1]], axis=1
)
out.columns = ['Time', 'Normal', 'Time', 'Abnormal']
out
               Time  Normal              Time  Abnormal
0  11/30/2011 22:50    74.3  11/30/2011 23:00      74.8
1  11/30/2011 23:30    74.8  11/30/2011 23:10      77.7
2  11/30/2011 13:00    80.9               NaN       NaN

这篇关于Python-用实际值替换分数(转置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 16:22