Original English: 06-lesson
Let's take a look at the groupby function.
# import library Import
pandas as PD
import sys
Print (' Python version ' + sys.version)
print (' Pandas version ' + pd.__version__)
Python version 3.6.1 | Packaged by Conda-forge | (Default, Mar 2017, 21:57:00)
[GCC 4.2.1 compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Pandas version 0.19.2
# Our decimal aggregates
D = {' One ': [1,1,1,1,1],
' two ': [2,2,2,2,2], ' Letter
': [' a ', ' a ', ' B ', ' B ', ' C ']}
# Create a Dataframe
df = PD. Dataframe (d)
DF
|
| Letter
| One
two |
0 |
A |
1 |
2 |
1 |
A |
1 |
2 |
2 |
B |
1 |
2 |
3 |
B |
1 |
2 |
4 |
C |
1 |
2 |
# Create a GroupBy object one
= df.groupby (' letter ')
# Apply the sum () function on a group
one.sum ()
|
| One
two |
Letter |
|
|
A |
2 |
4 |
B |
2 |
4 |
C |
1 |
2 |
LetterOne = Df.groupby ([' Letter ', ' one ']). SUM ()
LetterOne
|
|
Two |
| Letter
| One
|
a |
1 |
4 |
b |
1 |
4 |
C |
1 |
2 |
Letterone.index
Multiindex (levels=[[' A ', ' B ', ' C '], [1]],
labels=[[0, 1, 2], [0, 0, 0]],
names=[' letter ', ' one ']
You may not want to index the column names used for grouping, as is easy to do in the following ways.
LetterOne = Df.groupby ([' Letter ', ' one '], as_index=false). SUM ()
LetterOne
|
| Letter
| One
two |
0 |
A |
1 |
4 |
1 |
B |
1 |
4 |
2 |
C |
1 |
2 |
Letterone.index
Int64index ([0, 1, 2], dtype= ' Int64 ')