Tuesday, December 12, 2017

Pandas Kütüphanesi Ders - 6

Lesson 6



These tutorials are also available through an email course, please visit http://www.hedaro.com/pandas-tutorial to sign up today.
Lets take a look at the groupby function.
In [1]:
# Import libraries
import pandas as pd
import sys
In [2]:
print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)
Python version 3.5.1 |Anaconda custom (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)]
Pandas version 0.20.1
In [3]:
# Our small data set
d = {'one':[1,1,1,1,1],
     'two':[2,2,2,2,2],
     'letter':['a','a','b','b','c']}

# Create dataframe
df = pd.DataFrame(d)
df
Out[3]:
letteronetwo
0a12
1a12
2b12
3b12
4c12
In [4]:
# Create group object
one = df.groupby('letter')

# Apply sum function
one.sum()
Out[4]:
onetwo
letter
a24
b24
c12
In [5]:
letterone = df.groupby(['letter','one']).sum()
letterone
Out[5]:
two
letterone
a14
b14
c12
In [6]:
letterone.index
Out[6]:
MultiIndex(levels=[['a', 'b', 'c'], [1]],
           labels=[[0, 1, 2], [0, 0, 0]],
           names=['letter', 'one'])
You may want to not have the columns you are grouping by become your index, this can be easily achieved as shown below.
In [7]:
letterone = df.groupby(['letter','one'], as_index=False).sum()
letterone
Out[7]:
letteronetwo
0a14
1b14
2c12
In [8]:
letterone.index
Out[8]:
Int64Index([0, 1, 2], dtype='int64')
This tutorial was created by HEDARO

No comments:

Post a Comment

file tree for nodejs project

 find . \( -path "*/node_modules" -o -path "*/.git" \) -prune -o -print | tree -a -I 'node_modules|.git'