Resources
Answers
Examples
Sign in
Get Started
What do you want to save?
Add Code snippet
New code examples
Python
2022-01-30 01:41:47
how to select top 5 in every group pandas
df.groupby('id').apply(lambda x : x.sort_values(by = 'value', ascending = False).head(2).reset_index(drop = True)) #Alter native df.groupby(['id']).apply(lambda x: x.nlargest(topk,['value'])).reset_index(drop=True)
Add solution
Python
2022-01-27 13:56:33
pivot table but keep nan
(df.groupby(['Date', 'A']).B .apply(lambda x: np.nan if x.isna().all() else x.sum()) .unstack('A') )
Add solution
Python
2022-01-23 02:11:04
mode with group by in python
df.groupby(['ITEM', 'CATEGORY']).apply(pd.DataFrame.mode).reset_index(drop=True)
Add solution
Other
2022-01-22 21:40:03
Sum values of column based on the unique values of another column
df.groupby('Column1')['Column2'].sum() Column1 Column2 1 44 2 65 3 30 4 18
Add solution
Python
2022-01-22 14:50:20
nunique sort
df = df.groupby('A')['B'].nunique().sort_values(ascending=False).reset_index(name='count') print (df) A count 0 D 3 1 C 2 2 E 1
Add solution
Python
2022-01-22 06:40:30
how to sum the revenue from every day in a dataframe python
df.groupby('Date')['column_you_want_to_sum'].sum()
Add solution
Other
2021-11-19 18:26:13
agg custom column name
df.groupby(['col']).agg(func=func)
Add solution
Python
2021-11-18 09:58:39
sort values within groups pandas dataframe
import pandas as pd df = pd.DataFrame({'A':[1, 2, 3, 4, 5, 6], 'B':[0, 1, 2, 0, 1, 2]}) df.groupby('B').apply(lambda x: x.sort_values('A', ascending = True))
Add solution
Python
2021-11-17 17:22:07
groupby and list
In [1]: df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6]}) df Out[1]: a b 0 A 1 1 A 2 2 B 5 3 B 5 4 B 4 5 C 6 In [2]: df.groupby('a')['b'].apply(list) Out[2]: a A [1, 2] B [5, 5, 4] C [6] N...
Add solution
Python
2021-11-17 01:08:08
group by 2 columns pandas
grouped_multiple = df.groupby(['Team', 'Pos']).agg({'Age': ['mean', 'min', 'max']}) grouped_multiple.columns = ['age_mean', 'age_min', 'age_max'] grouped_multiple = grouped_multiple.reset_index() print(grouped_multiple)
Add solution
‹
1
2
3
4
5
6
7
›
Best helpers
daily
weekly
monthly
for 3 months
Darien Springer
+5
show more
This website uses cookies to make IQCode work for you. By using this site, you agree to our
cookie policy
Welcome Back!
Sign up to unlock all of IQCode features:
Test your skills and track progress
Engage in comprehensive interactive courses
Commit to daily skill-enhancing challenges
Solve practical, real-world issues
Share your insights and learnings
Sign in
E-mail
Password
Recover lost password
Log in
Or log in with
Not registered?
Sign up takes less than a minute
Create a Free Account
Sign up to unlock all of IQCode features:
Test your skills and track progress
Engage in comprehensive interactive courses
Commit to daily skill-enhancing challenges
Solve practical, real-world issues
Share your insights and learnings
Sign up
Email
Password
Sign up for free
Or sign up with
By signing up, you agree to the
Terms and Conditions
and
Privacy Policy
. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.