Python Series
Python Series
SERIES
What is a series in python?
1) Series is a labeled data structure.
2) It contains data and indices
3) It can hold int, strings, floats etc
4) It stores same type of data(homogenous data)
5) The DATA of series is mutable
6) Size of series cannot be changed (immutable)
Series creation:
We van create a series in many ways. Some of them are explained below
1) Empty series
2) Using Arguments
3) Using Dictionaries
4) Without Indexes
5) Using Scalar value
2) Using Arguments
import pandas as pd
s1= pd.Series(data=[10.9, 20.5, 30.6, 7.9], index=['a', 'b', 'c', 'd'])
print (s1)
OUTPUT:
a 10.9
b 20.5
c 30.6
d 7.9
dtype: float64
3) Using Dictionaries
import pandas as pd
d={'mon':1, 'tues':2, 'wed':3, 'thur':4, 'fri':5}
s1= pd.Series(d)
print (s1)
OUTPUT:
mon 1
tues 2
wed 3
thur 4
fri 5
dtype: int64
4) Without indexes
import pandas as pd
s1= pd.Series(data=[10.9, 20.5, 30.6, 7.9])
print (s1)
OUTPUT:
the sum of series is: 996
the no. values in the series is: 7
the largest value is 400
the smallest value is: 30
OUTPUT:
USE OF sort_values()
sort_values(): Its sorts the series data values in ascending values. (the original series
remains the same)
sort_values(ascending=False): Its sorts the series data values in descending values. (the
original series remains the same).
example:
OUTPUT
1)
OUTPUT:
55
2)
OUTPUT:
3)
OUTPUT:
Slicing in Series
OUTPUT:
2)
Ans: Start: 0
Stop: 5
Step is missing. By default, it is 1.
OUTPUT:
3)
Ans: Start: 2
Stop: 4
Step is missing. By default it is 1.
OUTPUT:
4)
Ans:
Start is 3
Stop is missing, so it will continue till last value (in top-down direction)
Step is missing, it would be +1. (Top to down)
OUTPUT:
5)
Ans: Start is missing, so it will start from first index (In top-down direction)
Stop is 6
Step is 2 (Top to down)
OUTPUT:
6)
Ans: Start: -4
Stop: missing (till last value in top to down direction)
Step is missing. By default it is 1. (top to down)
OUTPUT:
Before moving on to the next topic let’s look at the DEFAULT negative index and positive index of a
series
Ans: Start:4
Stop: 1
Step=-1 (negative, Down to top)
OUTPUT:
2)
Ans: Start: -1
Stop: -5
Step=-2 (negative, Down to top)
OUTPUT:
3)
Ans: Start: -1
Stop: Missing, it will be till last value in Down to top direction
Step=-2 (negative, Down to top)
OUTPUT:
OUTPUT:
series s
0 50
1 60
2 70
3 80
4 90
dtype: int64
series s2
0 54
1 64
2 74
3 84
4 94
dtype: int64
series s3
0 45
1 55
2 65
3 75
4 85
dtype: int64
series s4
0 2
1 0
2 1
3 2
4 0
dtype: int64
series s5
0 25.0
1 30.0
2 35.0
3 40.0
4 45.0
dtype: float64
series s6
0 2500
1 3600
2 4900
3 6400
4 8100
dtype: int64
When mathematical operators are applied between more than series, then operation
takes place between the values of CORRESPONDING INDEX only.
If index does not match, NaN is displayed in front of unmatched indexes
Example 1:
OUTPUT:
Example 2:
OUTPUT: