-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIndex_Select_Filter.py
72 lines (55 loc) · 2.21 KB
/
Index_Select_Filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 1 15:47:25 2020
@author: Abhinav
"""
#------------------------Index, Select & Filter--------------------------------
#Create dataframe :
import pandas as pd
#Create a DataFrame
d = {'Name':['Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine',
'Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine'],
'Exam':['Semester 1','Semester 1','Semester 1','Semester 1','Semester 1','Semester 1',
'Semester 2','Semester 2','Semester 2','Semester 2','Semester 2','Semester 2'],
'Subject':['Mathematics','Mathematics','Mathematics','Science','Science','Science',
'Mathematics','Mathematics','Mathematics','Science','Science','Science'],
'Score':[62,47,55,74,31,77,85,63,42,67,89,81]}
df = pd.DataFrame(d,columns=['Name','Exam','Subject','Score'])
df
#View a column of the dataframe in pandas:
df['Name']
#View two columns of the dataframe in pandas:
df[['Name','Score','Exam']]
#View first two rows of the dataframe in pandas:
df[0:2]
#-------Filter in Pandas dataframe:--------------
#View all rows where score greater than 70
df['Score'] > 70
df[df['Score'] > 70]
#View all the rows where score greater than 70 and less than 85
df[(df['Score'] > 70) & (df['Score'] < 85)]
#-----------------Select in Pandas dataframe-----------------------------------
#select row by using row number in pandas with .iloc
#.iloc [1:m, 1:n] – is used to select or index rows based on their position
#from 1 to m rows and 1 to n columns
# select first 2 rows
df.iloc[:2]
# or
df.iloc[:2,]
#select 3rd to 5th rows
df.iloc[2:5]
# or
df.iloc[2:5,]
#select all rows starting from third row
df.iloc[2:]
# or
df.iloc[2:,]
#Select column by using column number in pandas with .iloc
# select first 2 columns
df.iloc[:,:2]
#select first 1st and 4th columns
df.iloc[:,[0,3]]
#Select value by using row name and column name in pandas with .loc:
#.loc [[Row_names],[ column_names]] –used to select or index rows or columns based on their name
#select value by row label and column label using loc
df.loc[[1,2,3,4,5],['Name','Score']]