简体   繁体   English

按特定列中的值频率限制 DataFrame 行

[英]Limit DataFrame rows by value frequency in specific column

Essentially I have a basic dataframe, within this dataframe there is a 'Streaming Service' column.基本上我有一个基本的 dataframe,在这个 dataframe 中有一个“流媒体服务”列。 I want to limit the results to the first 5 records for each service provider.我想将结果限制为每个服务提供商的前 5 条记录。 In other words I want to limit this dataframe from possibly thousands of records of shows to just the last 5 of each Streaming service.换句话说,我想将这个 dataframe 从可能的数千条节目记录限制到每个流媒体服务的最后 5 条。

import pandas as pd
import numpy as np

data = {'Show Name': ['GameOfThrones', 'StrangerThings', 'Casual', ...], 
        'Streaming Service': ['HBO', 'Netflix', 'Hulu']}
df1 = pd.DataFrame(data)

What's the best approach to doing this?这样做的最佳方法是什么?

df1.groupby('Streaming Service').head(5)

I ended up coming up with my own solution.我最终想出了自己的解决方案。 Problem was over complicated:问题过于复杂:

service_dfs = []

for c in df['Streaming Service'].unique():
    df_c = df.loc[df[ 'Streaming Service'] == c].tail(100)
    service_dfs.append(df_c)
df = pd.concat(service_dfs)

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

相关问题 添加DataFrame列默认值时,如何将其限制为特定行? - When adding a DataFrame column default value, how do I limit it to specific rows? 基于列值的 DataFrame 中的 select 行,限制为 16384 行 - select rows from a DataFrame based on column value, limit to 16384 rows 向后填充 dataframe 列,其中填充的行数限制基于单元格的值,可能使用 bfill() 和 limit=x - Backwards fill dataframe column where limit of rows filled is based on value of cell, perhaps with bfill() and limit=x 计算一个值出现在 dataframe 列末尾的频率 - count the frequency that a value occurs in the end of a dataframe column 统计一个值在 dataframe 列中出现的频率 - Count the frequency that a value occurs in a dataframe column 统计一个值在dataframe(多列)中出现的频率 - Count the frequency that a value occurs in a dataframe (multiple column) 获取Python中DataFrame列值的频率 - Get frequency of value in DataFrame column in Python Python:在数据框中用相同的值填充特定列并删除无用的行 - Python : Fill a specific column with the same value in a Dataframe and remove the rows useless 如何删除带有条件的 Pandas DataFrame 行以保留特定列值 - How to drop Pandas DataFrame rows with condition to keep specific column value 如果特定列中的值不是 Pandas 数据框中的整数,则删除行 - Drop rows if value in a specific column is not an integer in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM