Apply a lambda and call sample
with param frac
:
In [2]:
df = pd.DataFrame({'a': [1,2,3,4,5,6,7],
'b': [1,1,1,0,0,0,0]})
grouped = df.groupby('b')
grouped.apply(lambda x: x.sample(frac=0.3))
Out[2]:
a b
b
0 6 7 0
1 2 3 1
More Related Contents:
- How can I pivot a dataframe?
- Get statistics for each group (such as count, mean, etc) using pandas GroupBy?
- How do I create a new column from the output of pandas groupby().sum()?
- GroupBy pandas DataFrame and select most common value
- Pandas GroupBy.apply method duplicates first group
- pandas GroupBy columns with NaN (missing) values
- Count unique values per groups with Pandas [duplicate]
- How to access pandas groupby dataframe by key
- group by pandas dataframe and select latest in each group
- Pandas GroupBy and select rows with the minimum value in a specific column
- why does pandas rolling use single dimension ndarray
- Group dataframe and get sum AND count?
- Pandas groupby with categories with redundant nan
- Is there an “ungroup by” operation opposite to .groupby in pandas?
- get first and last values in a groupby
- Pandas groupby and aggregation output should include all the original columns (including the ones not aggregated on)
- Get the row corresponding to the max in pandas GroupBy [duplicate]
- How to get number of groups in a groupby object in pandas?
- Aggregation in Pandas
- Pandas groupby multiple fields then diff
- Pandas groupby mean – into a dataframe?
- Generating random dates within a given range in pandas
- How to group a pandas dataframe by a defined time interval?
- How to drop duplicates based on two or more subsets criteria in Pandas data-frame
- Pandas groupby nlargest sum
- Pandas get frequency of item occurrences in a column as percentage [duplicate]
- Python Pandas How to assign groupby operation results back to columns in parent dataframe?
- What is the difference between pandas agg and apply function?
- Pandas number rows within group in increasing order
- What is the equivalent of SQL “GROUP BY HAVING” on Pandas?