没有唯一列的数据透视图数据框 [英] Pivot DataFrame with no unique column

查看:92
本文介绍了没有唯一列的数据透视图数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DataFrame看起来像这样,

df = 
index | A   | B   | C
0     |00456|text1|date1
1     |00443|text1|date2
2     |00456|text1|date3
3     |00231|text2|date4
4     |00231|text3|date1
5     |00456|text2|date1

我想结束以下内容

df =
index | Α   | B_1 |B_2  |B_3  |C_1...
0     |00456|text1|text1|text2|...
1     |00443|text1|NaN  |NaN  |...
2     |00231|text2|text3|NaN  |...

具有_1后缀或NaN值并不重要,我要实现的是根据 A 列的内容将所有值放在同一行中. /p>

实现此目标的最佳方法是什么?

请注意,我是使用一种我认为是过于复杂的方法来完成此操作的,该方法涉及看起来像这样的操作,

groups = df.groupby(["A"])
df = pd.concat((groups.A.apply(lambda x: pd.Series(data=x.values)).unstack(),
                groups.B.apply(lambda x: pd.Series(data=x.values)).unstack(),
                groups.C.apply(lambda x: pd.Series(data=x.values)).unstack()),
                keys = ['A', 'B' 'C'], axis=1)

解决方案

使用

Use GroupBy.cumcount for counter with reshape by DataFrame.set_index and DataFrame.unstack, last flatten MultiIndex in columns and convert index to column:

g = df.groupby('A').cumcount() + 1
df1 = df.set_index(['A', g]).unstack()
df1.columns = [f'{a}_{b}' for a, b in df1.columns]
df1 = df1.reset_index()
print (df1)
     A    B_1    B_2    B_3    C_1    C_2    C_3
0  231  text2  text3    NaN  date4  date1    NaN
1  443  text1    NaN    NaN  date2    NaN    NaN
2  456  text1  text1  text2  date1  date3  date1

这篇关于没有唯一列的数据透视图数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆