我正在玩耍并试图学习熊猫。目前停留在某一点,
数据 -
A B C
---------------------------
1 1 1
1 1 2
1 1 3
2 1 3
2 1 1
2 1 2
1 2 2
1 2 1
1 2 3
我的预期输出是,
A B C
---------------------------
1 1 1
1 1 2
1 1 3
2 1 1
2 1 2
2 1 3
1 2 1
1 2 2
1 2 3
与其他列相比,原始数据看起来要复杂得多。这只是该数据的简化版本。所以基本上我想要的是对 Col C 的数据进行排序,以获得(Col A 和 B 的组合唯一键)。
目前我想做的是,
contentIDs = data.B.unique()
for iD in contentIDs:
slots = data[data.B == iD].A.unique()
for s in slots:
slotData = data[(data.A == s) & (data.B == iD)]
sortedData = slotData.sort_values(['A', 'B', 'C'])
#Loop throug data to get to the index of sorted data and then replace unsorted data with sorted data.
我认为可能有更好的方法来做到这一点。所以在我继续我的循环逻辑之前,在这里询问是否有更好的方法。做这样简单的事情看起来很糟糕。
欢迎任何建议或指点。如有任何混淆,请随时发表评论。
df.groupby(['A','B'],sort=False)['C'].apply(lambda x:x.sort_values()).reset_index(level=['A','B'])
A B C
0 1 1 1
1 1 1 2
2 1 1 3
4 2 1 1
5 2 1 2
3 2 1 3
7 1 2 1
6 1 2 2
8 1 2 3
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句