Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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/7/elixir/2.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_Pandas - Fatal编程技术网

Python 我想转置一个有字符串的数据帧

Python 我想转置一个有字符串的数据帧,python,pandas,Python,Pandas,我想转置具有字符串值的数据帧 col1 col2 col3 col4 a 1 1 aa a 1 2 bb a 1 3 cc a 1 4 dd b 2 1 ee b 2 2 ff b 2 3 gg b 2 4 hh 我在下面提到了代码 df_new= pd.pivot_table(

我想转置具有字符串值的数据帧

col1    col2    col3    col4        
a   1   1   aa      
a   1   2   bb      
a   1   3   cc      
a   1   4   dd      
b   2   1   ee      
b   2   2   ff      
b   2   3   gg      
b   2   4   hh  
我在下面提到了代码

df_new= pd.pivot_table(df,values = 'col4', index = ['col1','col2'],
columns = ['col3'])
col1    col2    col3    col4        
a   1   1   aa      
a   1   2   bb      
a   1   3   cc      
a   1   4   dd      
b   2   1   ee      
b   2   2   ff      
b   2   3   gg      
b   2   4   hh  
可用输出

col1    col2    col3    col4        
a   1   1   aa      
a   1   2   bb      
a   1   3   cc      
a   1   4   dd      
b   2   1   ee      
b   2   2   ff      
b   2   3   gg      
b   2   4   hh  
期望输出

我认为您需要使用if独特的col1、col2和col3组合:

col1    col2    col3    col4        
a   1   1   aa      
a   1   2   bb      
a   1   3   cc      
a   1   4   dd      
b   2   1   ee      
b   2   2   ff      
b   2   3   gg      
b   2   4   hh  
如果可能重复,则聚合函数联接:

col1    col2    col3    col4        
a   1   1   aa      
a   1   2   bb      
a   1   3   cc      
a   1   4   dd      
b   2   1   ee      
b   2   2   ff      
b   2   3   gg      
b   2   4   hh  

向我们展示您的原始数据帧