Python Pandas - II
Summarizing Data
• The describe() function computes a summary
of statistics pertaining to the DataFrame
columns.
• print [Link]()
DataFrame Operations
Aggregation Function(agg())
It returns a reduced version of the data by
producing one summary result per group.
It can be also written as aggregate() or agg()
As parameter we have to pass the function
name like max,min,sum.
groupby() function
It rearranges data into groups based on some
specific criteria and stores the rearranged data
in a new group object.
We can use aggregate function with groupby()
function.
Data Pivoting
• It is a summarizing technique to rearrange the
columns and rows in a report.
Pandas library having two functions for pivoting
1. pivot()
2. pivot_table()
pivot()
This method creates a new dataframe after reshaping the
data based on column values.
This method takes 3 arguments: index, column and
values.
It is used without aggregation.
This method gives error if there are multiple entries for a
column value pointing to the same Index (row).
fillna() function
• This is used to fill values with missing values.
• NaN = ‘’
• Example:
fillna(‘ ’)
Pivoting using column with filtering
dropna() function
• The dropna() function is used to remove a row
or a column from a dataframe which has a
NaN or no values in it.
Drop columns where all element is NaN
• [Link](axis=‘columns’,how=‘all’)
Drop rows where all element is NaN
• [Link](axis=‘index’,how=‘all’)
Drop the columns where any of the
element is NaN
• [Link](axis=‘columns', how=‘any’)
Drop the rows where any of the
element is NaN
• [Link](axis=‘index', how=‘any’)
Keep those rows with at least two
element is NaN
• [Link](thresh=3)
pivot_table()
• It will create a new table by aggregating the
data.
• It can work with duplicate entries.
Pivoting using .csv file
Data Ranking
• Data Ranking produces ranking for each
element in the array of elements. In case of
ties, assigns the mean rank.
• import pandas as pd
• import numpy as np
• s = [Link]([Link](5),
index=list('abcde'))
• s['d'] = s['b'] # so there's a tie
• print [Link]()
• Rank optionally takes a parameter ascending
which by default is true; when false, data is
reverse-ranked, with larger values assigned a
smaller rank.
• Rank supports different tie-breaking methods,
specified with the method parameter −
• average − average rank of ed group
• min − lowest rank in the group
• max − highest rank in the group
• first − ranks assigned in the order they appear
in the array