You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas中对分组DataFrame按bikeid每3个选1个抽样

Solution to Sample Bikeids Instead of Individual Trips in Pandas

Got it, I totally get where you're coming from—using grouped.sample(frac=0.3) samples individual trips within each bikeid's group, which isn't what you want. You need to first select 1 out of every 3 unique bikeids, then pull all their trip records. Here's how to do it properly:

Step 1: Extract Unique Bikeids

First, get all distinct bikeids from your DataFrame—this gives you the list of groups you want to sample from:

unique_bikeids = df['bikeid'].unique()

Step 2: Sample the Bikeids

Next, sample 1/3 of these bikeids. Use pd.Series.sample() for this (adding random_state ensures your results are reproducible):

# Sample ~33% of bikeids (matches "1 out of 3")
sampled_bikeids = pd.Series(unique_bikeids).sample(frac=1/3, random_state=42)

Step 3: Filter Trips for Sampled Bikeids

Finally, filter your original DataFrame to keep only trips from the sampled bikeids:

sampled_df = df[df['bikeid'].isin(sampled_bikeids)]

Alternative Method (Using Groupby Directly)

If you prefer working with the groupby object directly, you can do this:

# Get all bikeid group keys
bikeid_group_keys = df.groupby('bikeid').groups.keys()
# Sample the keys
sampled_keys = pd.Series(list(bikeid_group_keys)).sample(frac=1/3, random_state=42)
# Concatenate the selected groups into a new DataFrame
sampled_df = pd.concat([df.groupby('bikeid').get_group(key) for key in sampled_keys])

Why This Works

Unlike grouped.sample(), which operates within each group, this approach samples the groups themselves first. Every trip from a selected bikeid is included in your final dataset, which aligns with your requirement of picking 1 bikeid out of every 3.

Example to Test

If you want to verify, here's a quick sample dataset you can use:

import pandas as pd
import numpy as np

# Create test data: 9 bikeids, 5 trips each
data = {
    'bikeid': np.repeat(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], 5),
    'trip_id': range(45),
    'distance': np.random.randint(1, 10, 45)
}
df = pd.DataFrame(data)

# Run the steps above
unique_bikeids = df['bikeid'].unique()
sampled_bikeids = pd.Series(unique_bikeids).sample(frac=1/3, random_state=42)
sampled_df = df[df['bikeid'].isin(sampled_bikeids)]

# Check the result: should have ~3 unique bikeids, all their trips
print(sampled_df['bikeid'].unique())

内容的提问来源于stack exchange,提问作者Vincent Cui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:19:00