按降序对两列最频繁的组合进行排序 [英] sort the most frequent combinations of two columns in descending order

查看:73
本文介绍了按降序对两列最频繁的组合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框

I have dataframe that looks like this

+---+---+---
|  A|  B| C|
+---+---+---
|  1|  3| 1|
|  2|  1| 1|
|  2|  3| 1|
|  1|  2| 1|
|  3|  1| 1|
|  1|  2| 1|
|  2|  1| 1|
|  1|  3| 1|
|  1|  2| 1|
+---+---+---

我想将数据减少为仅以降序排列的两列(A和B)的最频繁组合 输出应该看起来像

I want to reduce the data to only the most frequent combinations of two columns (A and B) sorted in descending order The output should look like

+---+---+-----+
|  A|  B|count|
+---+---+-----+
|  1|  2|    3|
|  2|  1|    2|
+---+---+-----+

我写了这段代码,但是没有排序

I wrote this code but it does not sort

import pandas as pd
import numpy as np
data=pd.read_csv("file.csv",sep=',')
gps = data[['A','B','C']]
gps1=gps.groupby(['A','C'])


gps1=gps1.count()
gps1.columns=['count']
gps1.sort_values(['count'],ascending=False)
print(gps1)

推荐答案

您需要将sort_values()的结果分配回gps1或使用`inplace = True:

You need to assign the result of sort_values() back into gps1 or use `inplace=True:

gps1.sort_values(['count'],ascending=False, inplace=True)

gps1 = gps1.sort_values(['count'],ascending=False)

sort_values

As stated in the documentation of sort_values, inplace is by default set to False

这篇关于按降序对两列最频繁的组合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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