Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

df = pd.DataFrame({'a':[1,1,1,2,2,2,3,3,3,3],'b':np.random.randn(10)})

df
   a         b
0  1  1.048099
1  1 -0.830804
2  1  1.007282
3  2 -0.470914
4  2  1.948448
5  2 -0.144317
6  3 -0.645503
7  3 -1.694219
8  3  0.375280
9  3 -0.065624

groups = df.groupby('a')

groups # Tells you what "df.groupby('a')" is, not an error
<pandas.core.groupby.DataFrameGroupBy object at 0x00000000097EEB38>

groups.count() # count the number of 1 present in the 'a' column
   b
a   
1  3
2  3
3  4

groups.sum() # sums the 'b' column values based on 'a' grouping

          b
a          
1  1.224577
2  1.333217
3 -2.030066
Comment

PREVIOUS NEXT
Code Example
Python :: gcd python 
Python :: pandas remove multi header from dataframe 
Python :: how to devided array into parts python 
Python :: check if a word is a noun python 
Python :: how to use re.sub 
Python :: How to check for string membership in python 
Python :: how to get a character from a string in python 
Python :: django add queury parameters to reverse 
Python :: python ceil method 
Python :: how to add condition if null value in django orm 
Python :: copy array along axis numpy 
Python :: local time in python 
Python :: tuple to string python 
Python :: python redis delete many 
Python :: serialize list to json python 
Python :: python how to use rnage 
Python :: delete plotted text in python 
Python :: dynamically create python dictionary 
Python :: python initialize multidimensional array 
Python :: How To Display An Image On A Tkinter Button 
Python :: defaultdict item count 
Python :: how to convert string into list in python 
Python :: Convert a Pandas Column of Timestamps to Datetimes 
Python :: how to check how many key value pairs are in a dict python 
Python :: Python Switch case statement by Dictionary Mapping 
Python :: python == vs is 
Python :: traversal tree in python 
Python :: lambda 
Python :: loops in python 
Python :: python math exp 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =