Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取包含所有行中数据的日志文件,并将其转换为基于列的输出?_Python_Python 2.7_Pandas_Dataframe_Pandas Groupby - Fatal编程技术网

Python 如何获取包含所有行中数据的日志文件,并将其转换为基于列的输出?

Python 如何获取包含所有行中数据的日志文件,并将其转换为基于列的输出?,python,python-2.7,pandas,dataframe,pandas-groupby,Python,Python 2.7,Pandas,Dataframe,Pandas Groupby,目前,我有一个如下所示的日志文件: PS V I P PSX1 1.802 .006 .011 PSX4 1.113 .179 .199 PSX2 1.111 .036 .041 PSX3 1.095 .003 .046 PSX1 1.802 .007 .014 PSX4 1.114 .180 .201 PSX2 1.111 .038 .041

目前,我有一个如下所示的日志文件:

 PS       V       I       P
PSX1    1.802   .006    .011
PSX4    1.113   .179    .199
PSX2    1.111   .036    .041
PSX3    1.095   .003    .046

PSX1    1.802   .007    .014
PSX4    1.114   .180    .201
PSX2    1.111   .038    .041
PSX3    1.096   .003    .005
我想在通过脚本后将它改成这样

   PSX1         PSX2         PSX3        PSX4
V   I   P    V   I   P    V   I   P    V   I   P
#   #   #    #   #   #    #   #   #    #   #   #
#   #   #    #   #   #    #   #   #    #   #   #
.   .   .    .   .   .    .   .   .    .   .   . 
.   .   .    .   .   .    .   .   .    .   .   . 

我一直在使用pandas来处理数据帧,但我不确定如何实现这一点,或者是否有可能实现这一点。任何帮助都将不胜感激。

使用
cumcount
创建新的索引键,然后我们使用
swaplevel
+
sort\u index

df=df.assign(newindex=df.groupby('PS').cumcount())
df.set_index(['newindex','PS']).unstack().swaplevel(0,1,axis=1).sort_index(level=0,axis=1)
Out[91]: 
PS         PSX1                 PSX2                 PSX3                \
              I      P      V      I      P      V      I      P      V   
newindex                                                                  
0         0.006  0.011  1.802  0.036  0.041  1.111  0.003  0.046  1.095   
1         0.007  0.014  1.802  0.038  0.041  1.111  0.003  0.005  1.096   
PS         PSX4                
              I      P      V  
newindex                       
0         0.179  0.199  1.113  
1         0.180  0.201  1.114  

df.set_index('PS').T
?这太完美了,非常感谢!有没有办法让它变为ip而不是ipv?如果没有,那没什么大不了的,我只是好奇。@MohitModi你可以使用类别sortI对不起,你说的类别排序是什么意思?@MohitModi检查问题