You are on page 1of 7

2/15/22, 6:28 PM Python | Math operations for Data analysis - GeeksforGeeks

Python | Math operations for Data analysis


Difficulty Level :
Easy ● Last Updated :
18 Aug, 2021

P ython is a great language for doing data analysis, primarily because of the fantastic

ecosystem of data-centric P ython packages. Pandas is one of those packages and

makes impor ting and analyzing data much easier.

There are some impor tant math operations that can be per formed on a pandas series to

simplif y data analysis using P ython and save a lot of time.

To get the data-set used, click here. 

s=read_csv("stock.csv", squeeze=True)

#reading csv file and making series

Function Use

s.sum() Returns sum of all values in the series

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Got It !
Policy ▲

https://www.geeksforgeeks.org/python-math-operations-for-data-analysis/ 1/9
2/15/22, 6:28 PM Python | Math operations for Data analysis - GeeksforGeeks

Function Use

Start Your Coding Journey Now! Login Register


s.mean() Returns mean of all values in series. Equals to s.sum()/s.count() 

Data Structures
s.std() Algorithms Interview Preparation
Returns standard deviation of all values Topic-wise Practice C++ Java Python

s.min() or Return min and max values from series

s.max()

s.idxmin() or Returns index of min or max value in series

s.idxmax()

s.median() Returns median of all value

s.mode() Returns mode of the series

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Got It !
Policy ▲

https://www.geeksforgeeks.org/python-math-operations-for-data-analysis/ 2/9
2/15/22, 6:28 PM Python | Math operations for Data analysis - GeeksforGeeks

Function Use

Start Your Coding Journey Now! Login Register


s.value_counts() Returns series with frequency of each value 

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Got It !
Policy ▲

https://www.geeksforgeeks.org/python-math-operations-for-data-analysis/ 3/9
2/15/22, 6:28 PM Python | Math operations for Data analysis - GeeksforGeeks

Function Use

Start Your Coding Journey Now! Login Register


s.describe() Returns a series with information like mean, mode, etc depending

on dtype of data passed 

Code #1: 

P ython3

# import pandas for reading csv file


import pandas as pd
 
#reading csv file
s = pd.read_csv("stock.csv", squeeze = True)
 
#using count function
print(s.count())
 
#using sum function
print(s.sum())
  use cookies to ensure you have the best browsing experience on our website. By using
We
#using mean function
our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Got It !
print(s.mean()) Policy ▲

https://www.geeksforgeeks.org/python-math-operations-for-data-analysis/ 4/9
2/15/22, 6:28 PM Python | Math operations for Data analysis - GeeksforGeeks

 
#calculation average
print(s.sum()/s.count())
 Start Your Coding Journey Now! Login Register
#using std function
print(s.std())
 
#using min function
print(s.min())
 
#using max function
print(s.max())
 
#using count function
print(s.median())
 
#using mode function
print(s.mode())

Output :  

3012

1006942.0

334.3100929614874

334.3100929614874

173.18720477113115

49.95

782.22

283.315

0 291.21

Code #2: 

P ython3

# import pandas for reading csv file


import pandas as pd
 
#reading csv file
s =usepd.read_csv("stock.csv",
We squeezeexperience
cookies to ensure you have the best browsing = True) on our website. By using
 
our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Got It !
#using describe function Policy ▲

https://www.geeksforgeeks.org/python-math-operations-for-data-analysis/ 5/9
2/15/22, 6:28 PM Python | Math operations for Data analysis - GeeksforGeeks

print(s.describe())
 
#using count function
Start Your Coding Journey Now!
print(s.idxmax()) Login Register
 
#using idxmin function
print(s.idxmin())
 
#count of elements having value 3
print(s.value_counts().head(3))

Output : 

dtype: float64

count 3012.000000

mean 334.310093

std 173.187205

min 49.950000

25% 218.045000

50% 283.315000

75% 443.000000

max 782.220000

Name: Stock Price, dtype: float64

3011

11

291.21 5

288.47 3

194.80 3

Name: Stock Price, dtype: int64

Unexpected Outputs and Restrictions :

1. .sum(), .mean(), .mode(), .median() and other such mathematical operations are

not applicable on string or any other data type than numeric value.

2. .sum() on a string series would give an unexpected output and return a string by

concatenating ever y string.


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Got It !
Policy ▲

https://www.geeksforgeeks.org/python-math-operations-for-data-analysis/ 6/9
2/15/22, 6:28 PM Python | Math operations for Data analysis - GeeksforGeeks

Start Your Coding Journey Now! Login Register

Like 2

Previous Next

RECOMMENDED ARTICLES Page : 1 2 3

RFM Analysis Analysis Using Python Data Analysis and Visualization


01 05
21, Oct 21
with Python | Set 2
20, Feb 18

Data analysis and Visualization


02
with Python Exploratory Data Analysis in Python
06
30, Dec 16
| Set 1
18, Jan 19

Analysis of test data using K-Means Exploratory Data Analysis in Python


03
Clustering in Python 07
| Set 2
07, Jan 18
18, Jan 19

Replacing strings with numbers in Olympics Data Analysis Using


We use cookies to ensure you have the best browsing experience on our website. By using
04 08
Python for Data
our site, you
acknowledge that youAnalysis
have read and understood our
CookiePython
Policy &
Privacy Got It !
31, Jan 18 Policy ▲ 23, Sep 21

https://www.geeksforgeeks.org/python-math-operations-for-data-analysis/ 7/9

You might also like