gpt4 book ai didi

python - 仅当值不同时才创建新列

转载 作者:太空狗 更新时间:2023-10-29 18:19:46 25 4
gpt4 key购买 nike

我的数据框是这样的:

pd.DataFrame([["t1","d2","e3","r4"],
["t1","d2","e2","r4"],
["t1","d2","e1","r4"]],columns=["a","b","c","d"])

我想要:

pd.DataFrame([["t1","d2","e3","r4","e1","e2"]],
columns=["a","b","c","d","c1","c2"])

即我只有 1 列值不同,我想创建一个新的数据框,在观察到新值时添加列。有没有简单的方法可以做到这一点?

最佳答案

编辑:对任何单个非唯一列进行概括:

Ucols = df.columns[(df.nunique() == 1)].tolist()
df_out = df.set_index(Ucols).set_index(df.groupby(Ucols).cumcount(), append=True).unstack()
df_out.columns = [f'{i}{j}' if j != 0 else f'{i}' for i,j in df_out.columns]
print(df_out.reset_index())

输出:

    a   b   d   c  c1  c2
0 t1 d2 r4 e3 e2 e1

原始答案

使用:

df_out = df.set_index(['a','b','d',df.groupby(['a','b','d']).cumcount()]).unstack()

df_out.columns = [f'{i}{j}' if j != 0 else f'{i}' for i,j in df_out.columns]

df_out.reset_index()

输出:

    a   b   d   c  c1  c2
0 t1 d2 r4 e3 e2 e1

关于python - 仅当值不同时才创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52480841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com