简体   繁体   English

如何使用pandas基于另一列查找数量前3个值

[英]How to find top 3 values in amount, based on another column by using pandas

在此处输入图片说明

  • A, B, C, D, E are games. A、B、C、D、E 是游戏。 There are 80 different games有80种不同的游戏
  • This dataframe has 5,000 rows此数据框有 5,000 行

How can I list top 3 games, in terms of amount?就数量而言,我如何列出前 3 款游戏?

首先将DataFrame.explode用于列表到标量,然后通过DataFrame.drop_duplicates删除重复DataFrame.drop_duplicates ,最后通过Series.value_countsSeries.head获得 top3,因为value_counts默认排序:

top3 = df.explode('Games').drop_duplicates(['Games','Room'])['Games'].value_counts().head(3)

Explode your Games columns (if Games contains real Python list) then drop duplicates (according your side notes) and use value_counts with different parameters according to what you want:展开您的Games列(如果Games包含真正的 Python 列表)然后删除重复项(根据您的附注)并根据您的需要使用具有不同参数的value_counts

  1. Top 3 for all rooms:所有房间的前 3 名:
>>> df.explode('Games') \
      .drop_duplicates(['Games', 'Rooms']) \
      .value_counts('Games').head(3)
Games
A    2
B    2
C    2
dtype: int64
  1. Top 3 per room:每间客房前三名:
>>> df.explode('Games') \
      .drop_duplicates(['Games', 'Rooms']) \
      .value_counts(['Games', 'Rooms']).head(3)
Games  Rooms
A      North    1
       West     1
B      East     1
dtype: int64

Setup:设置:

data = {'Games': [['A', 'B', 'C'], ['B', 'D'], ['B', 'E'], ['A', 'C'], ['D']],
        'Rooms': ['West', 'East', 'East', 'North', 'South']}
df = pd.DataFrame(data)
print(df)

# Output:
       Games  Rooms
0  [A, B, C]   West
1     [B, D]   East
2     [B, E]   East
3     [A, C]  North
4        [D]  South

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas - 如何根据另一列的条件找到 1 列的前 n 个元素 - Pandas - How do you find the top n elements of 1 column based on a condition from another column Pandas 根据另一列中的条件选择前 10 个值 - Pandas pick top 10 values based on condition in another column Pandas GroupBy:如何根据列获得前n个值 - Pandas GroupBy : How to get top n values based on a column 如何根据另一列找到列的前5个值? - How to find the top 5 values of a column according to another column? 使用 pandas 根据 Excel 中另一列中的值对列进行颜色编码 - Color code a column based on values in another column in Excel using pandas 使用函数根据另一列的值创建 Pandas 列 - Creating a Pandas column based on values of another column using function 如何在 pandas dataframe 中查找每一行的顶列值 - How to find the top column values of each row in a pandas dataframe 如何使用pandas基于另一列[SoldDate]找到特定列[Model]的计数? - How do I find the count of a particular column [Model], based on another column [SoldDate] using pandas? 熊猫:根据另一列查找一列中的前10个记录组合 - Pandas: Find top 10 combination of records in one column, based on another column 如何使用熊猫中的for循环根据另一列的条件填充一列中的缺失值? - How to fill in missing values in one column based on a condition form another column using for loops in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM