Ip Lab File Python
Ip Lab File Python
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
Another example:
# import pandas lib. as pd
import pandas as pd
# importing pandas
import pandas as pd
# multi-list
list = [ ['Geeks'], ['For'], ['Geeks'], ['is'],
['a'], ['portal'], ['for'], ['geeks'] ]
print(df)
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)
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)
# 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.
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
df
# 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)
]
# 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)
]
# 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)
]
# 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)
]
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'])
# 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)
]