Learning Pandas (vi)

Source: Internet
Author: User

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
Letter One
Two
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 ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.