0% found this document useful (0 votes)
22 views9 pages

Ip Lab File Python

Uploaded by

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

Ip Lab File Python

Uploaded by

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

INDEX

Sl. Experiment Name Page Number Date of Date of Remarks/Signature


Experiment submission
No. From To

1 Creating Series
 From List
 From arrays
 From Dictionary
2 Creating DataFrames
 From List
 From Dictionaries
 From tuples
3 Select Rows With Multiple
Filters in Pandas
4 Select Rows & Columns by
Name or Index in Pandas
DataFrame using [ ], loc[]
& iloc[]
5

10

11
Create Series from Lists & ndarrays
A Series is a one-dimensional labeled array capable of holding any data type (integers,
strings, floating point numbers, Python objects, etc.). It has to be remembered that unlike
Python lists, a Series will always contain data of the same type..
# import pandas as pd
import pandas as pd

# create Pandas Series with default index values


# default index ranges is from 0 to len(list) - 1
x = pd.Series(['Geeks', 'for', 'Geeks'])

# print the Series


print(x)

Method #2 : Using Series() method with 'index' argument.


# import pandas lib. as pd
import pandas as pd

# create Pandas Series with define indexes


x = pd.Series([10, 20, 30, 40, 50], index =['a', 'b', 'c', 'd', 'e'])

# print the Series


print(x)

Another example:
# import pandas lib. as pd
import pandas as pd

ind = [10, 20, 30, 40, 50, 60, 70]

lst = ['Geeks', 'for', 'Geeks', 'is',


'portal', 'for', 'geeks']

# create Pandas Series with define indexes


x = pd.Series(lst, index = ind)

# print the Series


print(x)
Method #3: Using Series() method with multi-list

# importing pandas
import pandas as pd

# multi-list
list = [ ['Geeks'], ['For'], ['Geeks'], ['is'],
['a'], ['portal'], ['for'], ['geeks'] ]

# create Pandas Series


df = pd.Series((i[0] for i in list))

print(df)

Creating Series from dictionary

Dictionary of some key and value pair for the series of values taking keys as index of series.
# import pandas as pd
import pandas as pd

# simple dict
dct = {'G':2,'E':4,'K':2,'S':2,
'F':1,'O':1,'R':1}

# forming series
s = pd.Series(dct)

# output
print(s)

Creating Series from Numpy array

The 1-D Numpy array of some values form the series of that values uses array index as
series index.
# import pandas as pd
import pandas as pd

# import numpy as np
import numpy as np

# numpy array
arr = np.array(['G','E','E','K','S','F',
'O','R','G','E','E','K','S'])

# forming series
s = pd.Series(arr)
# output
print(s)

Accessing Element from Series with Position :


In order to access the series element refers to the index number. Use the index operator [ ] to
access an element in a series. The index must be an integer. In order to access multiple
elements from a series, we use Slice operation.
Accessing first 5 elements of Series

# import pandas and numpy


import pandas as pd
import numpy as np

# creating simple array


data = np.array(['g','e','e','k','s','f', 'o','r','g','e','e','k','s'])
ser = pd.Series(data)

#retrieve the first element


print(ser[:5])

Accessing Element Using Label (index) :


In order to access an element from series, we have to set values by index label. A Series is like a fixed-size dictionary in that you can get
and set values by index label.
Accessing a single element using index label

# import pandas and numpy


import pandas as pd
import numpy as np

# creating simple array


data = np.array(['g','e','e','k','s','f', 'o','r','g','e','e','k','s'])
ser = pd.Series(data,index=[10,11,12,13,14,15,16,17,18,19,20,21,22])

# accessing a element using index element


print(ser[16])

Select Rows With Multiple Filters in Pandas


Selecting rows with logical operators i.e. AND and OR can be achieved easily with a combination of >, <,
<=, >= and == to extract rows with multiple filters. loc() is primarily label based, but may also be used with
a boolean array to access a group of rows and columns by label or a boolean array.
Dataset Used:
Creating a dataframe with columns Name, Class, Marks in English, Marks in Maths, and Marks in History.
We are going to use the below dataset for all operations:
Python
import pandas as pd
# initialize list of lists
data = [['John', 8, 7, 6, 5], ['Paul', 8, 3, 6, 4],
['Juli', 9, 10, 9, 9], ['Geeta', 9, 5, 4, 4]]

# Create the pandas DataFrame


df = pd.DataFrame(
data, columns=['Name', 'Class', 'English',
'Maths', 'History'])

# print dataframe.
print(df)
Below are various operations which implement the selection of rows with multiple filters:
 Selecting row with students having marks is English greater than 6 and marks is maths greater than 8.
df1 = df[(df.English>6) & (df.Maths>8)]
print(df1)

 Selecting rows with students having marks in English greater than equal to 5 or marks is history
greater than 7.
df1 = df[(df.English>=5) | (df.History>7)]
print(df1)

 Selecting rows with students of class 9 having marks in English greater than equal to 5 or marks is
history greater than 7.
df1 = df[(df.Class == 9) & ((df.English>=5) | (df.History>7))]
print(df1)
 Selecting row with students having marks in English less than equal to 5 and marks is maths less than
equal to 5 and marks is history less than equal to 5.

df1 = df[(df.English<=5) & (df.Maths<=5) & (df.History<=5)]


print(df1)
 Selecting rows with students of class 8 having marks in English less than equal to 5 or marks is maths
greater than 5 or marks is history less than equal to 5.
df1 = df[(df.Class == 8) & ((df.English<=5) | (df.Maths>5) | (df.History<=5))]
print(df1)

 Selecting rows with loc() having marks in English greater than 6 and marks in maths greater than 6.
df1 = df.loc[(df['English']>6) & (df['Maths']>6)]
print(df1)
 Selecting rows with loc() having students marks in English greater than 6 or marks in maths greater
than 4. We only display columns with Name and Class.
df1 = df.loc[((df['English']>6) | (df['Maths']>4)),['Name','Class']]
print(df1)

Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
Indexing in Pandas means selecting rows and columns of data from a Dataframe. It can be selecting all
the rows and the particular number of columns, a particular number of rows, and all the columns or a
particular number of rows and columns each. Indexing is also known as Subset selection.
Let’s create a simple dataframe with a list of tuples, say column names are: ‘Name’, ‘Age’, ‘City’ and
‘Salary’.

# import pandas
import pandas as pd

# List of Tuples

employees = [('Stuti', 28, 'Varanasi', 20000),


('Saumya', 32, 'Delhi', 25000),
('Aaditya', 25, 'Mumbai', 40000),
('Saumya', 32, 'Delhi', 35000),
('Saumya', 32, 'Delhi', 30000),
('Saumya', 32, 'Mumbai', 20000),
('Aaditya', 40, 'Dehradun', 24000),
('Seema', 32, 'Delhi', 70000)
]

# Create a DataFrame object from list

df = pd.DataFrame(employees, columns =['Name', 'Age', 'City', 'Salary'])

# Show the dataframe

df

Example 2: to select multiple columns.

# import pandas
import pandas as pd

# List of Tuples
employees = [('Stuti', 28, 'Varanasi', 20000),
('Saumya', 32, 'Delhi', 25000),
('Aaditya', 25, 'Mumbai', 40000),
('Saumya', 32, 'Delhi', 35000),
('Saumya', 32, 'Delhi', 30000),
('Saumya', 32, 'Mumbai', 20000),
('Aaditya', 40, 'Dehradun', 24000),
('Seema', 32, 'Delhi', 70000)
]

# Create a DataFrame object from list


df = pd.DataFrame(employees,columns =['Name', 'Age', 'City', 'Salary'])

# Using the operator [] to


# select multiple columns
result = df[["Name", "Age", "Salary"]]

# Show the dataframe


result

Method 2: Using Dataframe.loc[ ].


.loc[] the function selects the data by labels of rows or columns. It can select a subset of rows and
columns. There are many ways to use this function.
Example 1: To select single row.

# import pandas
import pandas as pd

# List of Tuples
employees = [('Stuti', 28, 'Varanasi', 20000),
('Saumya', 32, 'Delhi', 25000),
('Aaditya', 25, 'Mumbai', 40000),
('Saumya', 32, 'Delhi', 35000),
('Saumya', 32, 'Delhi', 30000),
('Saumya', 32, 'Mumbai', 20000),
('Aaditya', 40, 'Dehradun', 24000),
('Seema', 32, 'Delhi', 70000)
]

# Create a DataFrame object from list


df = pd.DataFrame(employees,columns =['Name', 'Age','City', 'Salary'])

# Set 'Name' column as index


# on a Dataframe
df.set_index("Name", inplace = True)

# Using the operator .loc[]


# to select single row
result = df.loc["Stuti"]

# Show the dataframe


result
Example 2: To select multiple rows.
Code:
# import pandas
import pandas as pd

# List of Tuples
employees = [('Stuti', 28, 'Varanasi', 20000),
('Saumya', 32, 'Delhi', 25000),
('Aaditya', 25, 'Mumbai', 40000),
('Saumya', 32, 'Delhi', 35000),
('Saumya', 32, 'Delhi', 30000),
('Saumya', 32, 'Mumbai', 20000),
('Aaditya', 40, 'Dehradun', 24000),
('Seema', 32, 'Delhi', 70000)
]

# Create a DataFrame object from list


df = pd.DataFrame(employees,columns =['Name', 'Age','City', 'Salary'])

# Set index on a Dataframe


df.set_index("Name",
inplace = True)

# Using the operator .loc[]


# to select multiple rows
result = df.loc[["Stuti", "Seema"]]

# Show the dataframe


result
Example 3: To select multiple rows and particular columns.

# import pandas
import pandas as pd

# List of Tuples
employees = [('Stuti', 28, 'Varanasi', 20000),
('Saumya', 32, 'Delhi', 25000),
('Aaditya', 25, 'Mumbai', 40000),
('Saumya', 32, 'Delhi', 35000),
('Saumya', 32, 'Delhi', 30000),
('Saumya', 32, 'Mumbai', 20000),
('Aaditya', 40, 'Dehradun', 24000),
('Seema', 32, 'Delhi', 70000)
]

# Create a DataFrame object from list


df = pd.DataFrame(employees,columns =['Name', 'Age','City', 'Salary'])

# Set 'Name' column as index


# on a Dataframe
df.set_index("Name", inplace = True)

# Using the operator .loc[] to


# select multiple rows with some
# particular columns
result = df.loc[["Stuti", "Seema"],
["City", "Salary"]]

# Show the dataframe


result

Example 4: To select all the rows with some particular columns. We use single colon [ : ] to select all
rows and list of columns which we want to select as given below :

# import pandas
import pandas as pd

# List of Tuples
employees = [('Stuti', 28, 'Varanasi', 20000),
('Saumya', 32, 'Delhi', 25000),
('Aaditya', 25, 'Mumbai', 40000),
('Saumya', 32, 'Delhi', 35000),
('Saumya', 32, 'Delhi', 30000),
('Saumya', 32, 'Mumbai', 20000),
('Aaditya', 40, 'Dehradun', 24000),
('Seema', 32, 'Delhi', 70000)
]
# Creating a DataFrame object from list
df = pd.DataFrame(employees,columns =['Name', 'Age','City', 'Salary'])

# Set 'Name' column as index


# on a Dataframe
df.set_index("Name", inplace = True)

# Using the operator .loc[] to


# select all the rows with
# some particular columns
result = df.loc[:, ["City", "Salary"]]

# Show the dataframe


result

Example 3: to select multiple rows with some particular columns.

# import pandas
import pandas as pd

# List of Tuples
employees = [('Stuti', 28, 'Varanasi', 20000),
('Saumya', 32, 'Delhi', 25000),
('Aaditya', 25, 'Mumbai', 40000),
('Saumya', 32, 'Delhi', 35000),
('Saumya', 32, 'Delhi', 30000),
('Saumya', 32, 'Mumbai', 20000),
('Aaditya', 40, 'Dehradun', 24000),
('Seema', 32, 'Delhi', 70000)
]

# Creating a DataFrame object from list


df = pd.DataFrame(employees,
columns =['Name', 'Age',
'City', 'Salary'])

# Using the operator .iloc[]


# to select multiple rows with
# some particular columns
result = df.iloc[[2, 3, 5],
[0, 1]]

# Show the dataframe


result

You might also like