0% found this document useful (0 votes)
7 views18 pages

Data Frame and Series

Uploaded by

Nivedika Namburi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Data Frame and Series

Uploaded by

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

DATA FRAME AND

SERIES
UNIT 5(PANDAS LIBRARY)
INTRODUCTION TO SERIES

• A Pandas Series is like a column in a table.


• It is a one-dimensional array holding data of any type.

Create series :
Import pandas as pd
S= pd.Series([1,2,3])
Print(S)

OUTPUT:
0 1
1 2
2 3
CREATE A SIMPLE PANDAS SERIES FROM A
LIST:
Example code:
• import pandas as pd
• a = [1, 7, 2]
• myvar = pd.Series(a)
• print(myvar)

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.
• print(myvar[0]) #return first element of the series
CREATE LABELS

• With the index argument, you can name your


own labels. Return the value of "y":
Example: Create your own labels: print(myvar["y"])

• import pandas as pd
• a = [1, 7, 2]
• myvar = pd.Series(a, index = ["x", "y", "z"])
• print(myvar)
CREATE A SIMPLE PANDAS SERIES FROM A
DICTIONARY:
• import pandas as pd

calories = {"day1": 420, "day2": 380, "day3": 390}

myvar = pd.Series(calories)

print(myvar)

Note: The keys of the dictionary become the labels.


EXAMPLE

• Create a Series using only data from "day1" and "day2":


• import pandas as pd

calories = {"day1": 420, "day2": 380, "day3": 390}

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

print(myvar)
CREATE A SERIES FROM SCALAR

If data is a scalar value, an index must be provided. The value


will be repeated to match the length of index

#import the pandas library and aliasing as pd


import pandas as pd
import numpy as np
s = pd.Series(5, index=[0, 1, 2, 3])
print (s)
ACCESSING DATA FROM SERIES WITH
POSITION
• Data in the series can be accessed similar to that in an ndarray.
• Retrieve the first element. As we already know, the counting starts
from zero for the array, which means the first element is stored at
zeroth position and so on.
• import pandas as pd
• s = pd.Series([1,2,3,4,5])
• #retrieve the first element
• print(s[0])
• Example 2
• Retrieve the first three elements in the Series. If a : is inserted in front of it, all items from that index
onwards will be extracted. If two parameters (with : between them) is used, items between the two
indexes (not including the stop index)

• import pandas as pd
• s = pd.Series([1,2,3,4,5])
• #retrieve the first three element
• print s[:3]
• Example 3-Retrieve the last three elements.
• import pandas as pd
• s = pd.Series([1,2,3,4,5])
• #retrieve the last three element
• print s[-3:]
RETRIEVE DATA USING LABEL (INDEX)
CONVERT PANDA SERIES TO LIST

• Pandas tolist() is used to convert a series to list

• import pandas as pd
• ds = pd.Series([2, 4, 6, 8, 10])
• print("Pandas Series and type")
• print(ds)
• print(type(ds))
• print("Convert Pandas Series to Python list")
• print(ds.tolist())
• print(type(ds.tolist()))
CONVERT A GIVEN SERIES TO AN ARRAY.

Pandas Series.to_numpy() function is used to return a NumPy


ndarray representing the values in given Series or Index
import pandas as pd
• ds1 = pd.Series([2, 4, 6, 8, 10])
• A=ds1.to_numpy()
• print(A)
ARITHMETIC OPERATIONS ON SERIES

• import pandas as pd
print("Multiply two Series:")
• ds1 = pd.Series([2, 4, 6, 8, 10]) mul= ds1 * ds2
• ds2 = pd.Series([1, 3, 5, 7, 9]) print(mul)
print("Divide Series1 by Series2:")
• add = ds1 + ds2 div = ds1 / ds2
• print("Add two Series:") print(div)
• print(add)
• print("Subtract two Series:")
• sub = ds1 - ds2
• print(sub)
INTRODUCTION TO DATA FRAME
INTRODUCTION TO DATA FRAME

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


DataFrames.
• Series is like a column, a DataFrame is the whole table.
CREATING DATA FRAME FROM DISCTIONARY
CREATE EMPTY DATAFRAME
CREATING DATAFRAME USING LIST

• import pandas as pd
• stu_data = [(1,'Akshit',19,'male','jammu'),
• (2,'Samer',20,'male','Syria'),
• (3,'Harsh',20,'male','UP'),
• (4,'Nirmaljeet kaur',28,'female','Hoshiarpur'),
• (5,'jeremiah',18,'male','zambia')]
• df =
pd.DataFrame(stu_data,columns=['reg_id','Name','Age','Gender','City'])
• df

You might also like