Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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/9/opencv/3.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 - Fatal编程技术网

Python 通过在数据帧中旋转数据,将具有相同名称的列重新排列为一列

Python 通过在数据帧中旋转数据,将具有相同名称的列重新排列为一列,python,python-2.7,pandas,dataframe,Python,Python 2.7,Pandas,Dataframe,我有一个具有以下结构的数据帧: ID Material Description color size dim color size dim Tech 1 xcv456 Rubber 101 s 32 102 m 34 elastic 我想将其转换为: ID Material Description color size dim tech 1 xcv456 Rubber 101 s 32 elastic 1 xcv456

我有一个具有以下结构的数据帧:

ID Material Description color size dim color size dim Tech
1  xcv456    Rubber       101   s   32  102    m   34  elastic
我想将其转换为:

ID Material Description color size dim tech
1  xcv456   Rubber       101   s    32  elastic
1  xcv456   Rubber       102   m    34  elastic
我有一个5行5414列的文件,所以我试图自动化程序检测冗余列并将其转换为所需输出格式的过程。非常感谢您的帮助

使用:

#mask for all duplicates columns
m = df.columns.duplicated(keep=False)
#set index with not dupe columns
df = df.set_index(df.columns[~m].tolist())
#count dupes for MultiIndex
s = df.columns.to_series()
df.columns = [df.columns, s.groupby(s).cumcount()]
#reshape and remove 4 level, because 4 non dupe columns
df = df.stack().reset_index(level=4, drop=True).reset_index()
print (df)
   ID Material Description     Tech  color  dim size
0   1   xcv456      Rubber  elastic    101   32    s
1   1   xcv456      Rubber  elastic    102   34    m


在使用
pd.wide\u to\u Long之前需要一点处理

hh=pd.Series(df.columns)
df.columns=hh+hh.groupby(hh).cumcount().add(1).astype(str)
pd.wide_to_long(df,['color','size','dim'],i=['ID1','Material1','Description1','Tech1'],j='drop').reset_index().drop('drop',1
                                                                                                                )
Out[556]: 
   ID1 Material1 Description1    Tech1  color size  dim
0    1    xcv456       Rubber  elastic    101    s   32
1    1    xcv456       Rubber  elastic    102    m   34

@Bharathshetty-谢谢我有空行。哦,现在我看到了,可能是因为我使用了pd.read_剪贴板,在最后添加了.1的重复列。对不起,我的错。我在想这行不通。我想我应该发布一个问题来解决这个问题。
hh=pd.Series(df.columns)
df.columns=hh+hh.groupby(hh).cumcount().add(1).astype(str)
pd.wide_to_long(df,['color','size','dim'],i=['ID1','Material1','Description1','Tech1'],j='drop').reset_index().drop('drop',1
                                                                                                                )
Out[556]: 
   ID1 Material1 Description1    Tech1  color size  dim
0    1    xcv456       Rubber  elastic    101    s   32
1    1    xcv456       Rubber  elastic    102    m   34