Docsity

Python pandas 2 dataframe, Schemes and Mind Maps of Computer science

Partial preview of the text

Download Python pandas 2 dataframe and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Python Pandas -

• Iteration over DataFrame

• Binary Operations

• Statistics Function on DataFrame

• Some Essential Functions

• Handling Missing Data

• Groupby() Function

1. Iteration over DataFrame

If we want to access record or data from a data frame row wise or column wise then

iteration is used. Pandas provide 2 functions to perform iterations-

iterrows ()

iteritems ()

iterrows()

It is used to access the data row wise. Example-

iteritems() : It is used to access the data column wise.

Example-

values of df

which are

processed

one by one.

Try the code given below

after creation of DataFrame.

Binary Addition

DataFrame follows index matching to perform arithmetic operations. If matches,

operation takes place otherwise it shows NaN (Not a Number). It is called Data

Alignment in panda object. This behavior of ‘data alignment’ on the basis of “matching

indexes” is called MATCHING.

Binary Subtraction :

See the operation

of the rdiv

carefully

Descriptive Statistics Functions :

Descriptive statistics are used to describe / summarize large data in ways that are meaningful

and useful. Means “must knows” with any set of data. It gives us a general idea of trends in

our data including:

  • The mean, mode, median and range.
  • Variance and standard deviation ,quartile
  • SumCount, maximum and minimum.

Descriptive statistics is useful because it allows us take decision.

Steps to Get the descriptive statistics

  • Step 1: Collect the Data (Either from data file or from user)
  • Step 2: Create the DataFrame (Create dataframe from pandas object)
  • Step 3: Get the Descriptive Statistics for Pandas DataFrame

Pandas library come up with the methods to calculate max, min, count, sum, mean, median,

mode, quartile, Standard deviation, variance.

Note :- Dataframe object is best for descriptive statistics as it can hold large amount of data

and relevant functions.

Example : program for data aggregation/descriptive statistics

Other important functions of DataFrame are as under-

.info ( )

.describe ( )

Sorting - dataframe

Sorting means arranging the contents in ascending or

descending order.There are two kinds of sorting

available in pandas(Dataframe).

1. By value(column)

2. By index

1. By value - Sorting over dataframe column/s

elements is supported by sort_values() method. We

will cover here three aspects of sorting values of

dataframe.

• Sort a pandas dataframe in python by Ascending and

Descending

• Sort a python pandas dataframe by single column

• Sort a pandas dataframe by multiple columns.

Sorting - dataframe

Sort the python pandas Dataframe by single column – Ascending order

import pandas as pd

import numpy as np

#Create a Dictionary of series

d = {'Name':pd.Series(['Sachin','Dhoni','Virat','Rohit','Shikhar']),

'Age':pd.Series([26,27,25,24,31]),

'Score':pd.Series([87,89,67,55,47])}

#Create a DataFrame

df = pd.DataFrame(d)

print("Dataframe contents without sorting")

print (df)

df=df.sort_values(by='Score')

print("Dataframe contents after sorting")

print (df)

#In above example dictionary object is used to create

the dataframe.Elements of dataframe object df is s

orted by sort_value() method.As argument we are

passing value score for by parameter only.by default

it is sorting in ascending manner.

OUTPUT

Dataframe contents without sorting Name Age Score 1 Sachin 26 87 2 Dhoni 27 89 3 Virat 25 67 4 Rohit 24 55 5 Shikhar 31 47

Dataframe contents after sorting Name Age Score

3 Rohit 24 2 Virat 25 1 Dhoni 27 0 Sachin 26

4 Shikhar 31 47 55 67 87 89

Sorting - dataframe

Sort the pandas Dataframe by Multiple Columns

import pandas as pd

import numpy as np

#Create a Dictionary of series

d = {'Name':pd.Series(['Sachin','Dhoni','Virat','Rohit','Shikhar']),

'Age':pd.Series([26,25,25,24,31]),

'Score':pd.Series([87,67,89,55,47])}

#Create a DataFrame

df = pd.DataFrame(d)

print("Dataframe contents without sorting")

print (df)

df=df.sort_values(by=['Age', 'Score'],ascending=[True,False])

print("Dataframe contents after sorting")

print (df)

#In above example dictionary object is used to create

the dataframe.Elements of dataframe object df is s

orted by sort_value() method.we are passing two columns

as by parameter value and in ascending parameter also

with two parameters first true and second false,which

means sort in ascending order of age and descending

order of score

OUTPUT

Dataframe contents without sorting Name Age Score 1 Sachin 26 87 2 Dhoni 25 67 3 Virat 25 89 4 Rohit 24 55 5 Shikhar 31 47

Dataframe contents after sorting Name Age Score 3 Rohit 24 55 2 Virat 25 89 1 Dhoni 25 67 0 Sachin 26 87 4 Shikhar 31 47

Sorting - dataframe

2. By index - Sorting over dataframe index sort_index() is

supported by sort_values() method. We will cover here three

aspects of sorting values of dataframe. We will cover here

two aspects of sorting index of dataframe.

• how to sort a pandas dataframe in python by index in

Ascending order

• how to sort a pandas dataframe in python by index in

Descending order