2016-12-25 94 views
1

如何合并重复的DataFrame列并保留所有原始列名称?熊猫合并保留列名的重复DataFrame列

例如如果我有数据帧

df = pd.DataFrame({"col1" : [0, 0, 1, 2, 5, 3, 7], 
        "col2" : [0, 1, 2, 3, 3, 3, 4], 
        "col3" : [0, 1, 2, 3, 3, 3, 4]}) 

我可以删除重复的列(是的转慢大型DataFrames)与

df.T.drop_duplicates().T 

但这仅保留每个唯一列一个名

col1 col2 
0 0 0 
1 0 1 
2 1 2 
3 2 3 
4 5 3 
5 3 3 
6 7 4 

如何保存关于哪些列合并的信息?例如像

[col1] [col2, col3] 
0  0   0 
1  0   1 
2  1   2 
3  2   3 
4  5   3 
5  3   3 
6  7   4 

谢谢!

+2

公平的警告:你可能不希望存储列标题像期望的结果。标题并不是列表。如果你有12个重复列? – Parfait

回答

2
# group columns by their values 
grouped_columns = df.groupby(list(df.values), axis=1).apply(lambda g: g.columns.tolist()) 

# pick one column from each group of the columns 
unique_df = df.loc[:, grouped_columns.str[0]] 

# make a new column name for each group, don't think the list can work as a column name, you need to join them 
unique_df.columns = grouped_columns.apply("-".join) 

unique_df 

enter image description here

1

我也用Ttuplegroupby

def f(x): 
    d = x.iloc[[0]] 
    d.index = ['-'.join(x.index.tolist())] 
    return d 

df.T.groupby(df.apply(tuple), group_keys=False).apply(f).T 

enter image description here