لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science Pandas Series

What is a Series?

A Pandas Series is like a column in a table. It is a one-dimensional array holding data of any type.

How to create a simple Pandas Series from a list?

Example 1: Create a simple Pandas Series from a list.

Code

import pandas as pd

a = [2, 5, 8]

myvar = pd.Series(a)

print(myvar)

the output will be

0    2
1    5
2    8
dtype: int64

Labels

If nothing else is specified, the values are labeled with their index number.
First value has index 0, second value has index 1 etc.
This label can be used to access a specified value.

Example 2: Return the first value of the Series.

Code

import pandas as pd

a = [2, 5, 8]

myvar = pd.Series(a)

print(myvar[0])

the output will be

2

Creating Labels: With the index argument, you can name your own labels.

Example 3: Create you own labels.

Code

import pandas as pd

a = [2, 5, 8]

myvar = pd.Series(a, index = ["x", "y", "z"])

print(myvar)

the output will be


x    2
y    5
z    8
dtype: int64
Once you have created labels, you can access an item by referring to the label.

Example 4: Return the value of "z".

Code

import pandas as pd

a = [2, 5, 8]

myvar = pd.Series(a, index = ["x", "y", "z"])

print(myvar["z"])

the output will be

8

Key/Value Objects as Series: You can also use a key/value object, like a dictionary, when creating a Series.

Example 5: Create a simple Pandas Series from a dictionary.

Code

import pandas as pd

Score = {"day1": 200, "day2": 380, "day3": 500}

myvar = pd.Series(Score)

print(myvar)

the output will be

day1    200
day2    380
day3    500
dtype: int64

Note: Here in this case the keys of the dictionary become the labels.

In order to select only some of the items in the dictionary,
Use the index argument and specify only the items you want to include in the Series.

Example 6: From the above example create a Pandas Series using only data from "day1" and "day2".

Code

import pandas as pd

Score = {"day1": 200, "day2": 380, "day3": 500}

myvar = pd.Series(Score, index = ["day1", "day2"])

print(myvar)

the output will be

day1    200
day2    380
dtype: int64

DataFrames

Data sets in Pandas are usually multi-dimensional tables, called DataFrames.

Series is like a column, a DataFrame is the whole table.

Example 7: Create a DataFrame from two Series.

Code

import pandas as pd

data = {

"Movies": [1, 2, 3],

"Duration": [120, 150, 240]

}

myvar = pd.DataFrame(data)

print(myvar)

the output will be

   Movies  Duration
0       1       120
1       2       150
2       3       240

Example 8: Create a DataFrame from two Series Example 7(above example) With Your Own Labels x, y, z.

Code

import pandas as pd

data = {

"Movies": [1, 2, 3],

"Duration": [120, 150, 240]

}

myvar = pd.DataFrame
(data, index = ["x", "y", "z"]) print(myvar) the output will be

   Movies  Duration
x       1       120
y       2       150
z       3       240