0% found this document useful (0 votes)
16 views11 pages

Python Series

The document discusses Pandas and how it can be used to handle and analyze data in Python. It covers Pandas Series, how to create and access elements of a Series, slicing Series, and mathematical operations on one or more Series like sum, min, max, sorting.

Uploaded by

Hitu BGMI Hacker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Python Series

The document discusses Pandas and how it can be used to handle and analyze data in Python. It covers Pandas Series, how to create and access elements of a Series, slicing Series, and mathematical operations on one or more Series like sum, min, max, sorting.

Uploaded by

Hitu BGMI Hacker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

DATA HANDLING USING PANDAS

 It is a popular software library written for the Python Programming


language for data manipulation and analysis.
 While working with Pandas, it is required to import Pandas
 import pandas as pd
Here, pd is nick name given by programmer to pandas library
 Pandas provide 3 data structures
Pandas
Series Dataframe Panel

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

1) Creating an empty series:


import pandas as pd
s1= pd.Series()
print (s1)

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: (default indices start with 0,1,2.........)


0 10.9
1 20.5
2 30.6
3 7.9
dtype: float64

5) Using Scalar Values


import pandas as pd
s1=pd.Series(10, index=['a', 'b', 'c', 'd'])
print(s1)
#We have given a value 10, it will be repeated for al the indices given in the series
OUTPUT:
a 10
b 10
c 10
d 10
dtype: int64
FUNCTIONS in Series

USE OF sum(), count(), min() and max()


 sum(): It displays the sum of all values
 min(): It displays the smallest values of the series
 max(): It displays the largest value of the series
 count(): It displays the no. of values in the series(it does not count NaN values)
example:

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

USE OF head(), tail()


head(): It displays the first five values of the series
tail(): It displays the last five values of the series.
head(n): It displays the first n values.
tail(n): It displays the last n values.
example:

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

Accessing Series Elements using Index


We can retrieve the individual data members of the series using index.
Syntax: SeriesName[index]

1)

OUTPUT:
55

2)

OUTPUT:

3)

OUTPUT:

Slicing in Series

We can extract a portion/slice of a series using specifying the following indexes


SeriesName[Start:Stop:Step]

Start: Index of element where we start the slice


Stop: We stop the slice at stop-1 index.
Step: Gap between the elements. If Step is not specified, by default it is +1 (positive
one)

i) Slicing with positive Step Value: (Top to Down Always)


EXAMPLES:
1)
Ans: Start: 0
Stop: 5
Step is positive, so move top to down. Note that’s step is 2 so pick every second element

OUTPUT:

2)

Ans: Start: 0
Stop: 5
Step is missing. By default, it is 1.

OUTPUT:

3)

Ans: Step is missing. By default it is 1.


OUTPUT:

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

Negative index Positive Index Data


-5 0 1000
-4 1 2000
-3 2 3000
-2 3 4000
-1 4 5000

ii) Slicing with negative Step Value: (Top to Down Always)


1)

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:

Mathematical Operations on one series

Following are the mathematical operators that can be applied to


the series.
+, -, *, /, **, //, %
Example:

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

Mathematical Operations on more than one series

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:

Note: NaN is displayed against the unmatched indexes

You might also like