-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_filter.py
More file actions
69 lines (54 loc) · 1.76 KB
/
select_filter.py
File metadata and controls
69 lines (54 loc) · 1.76 KB
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
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)
print(df)
print()
#print(df.iloc[:8,:])
print('View a column of the dataframe in pandas:')
#print(df['Name'])
#print()
print(df.loc[:,'Name'])
print('View two columns of the dataframe in pandas:')
#print(df[['Name','Score']])
#print()
print(df.loc[:, ['Name', 'Score']])
print('To View first two rows of the dataframe in pandas:')
#print(df[:2])
print(df.iloc[:2,])
#Filter
print('all rows where score greater than 70')
print(df[df['Score'] > 70])
print()
print('print all the rows where score greater than 70 and less than 85')
print(df[(df['Score'] > 70) & (df['Score'] < 85)])
print()
#Select
print('select first 2 rows')
#print(df.iloc[:2])
# or
print(df.iloc[:2,])
print('to select 3rd to 5 th rows')
#print(df.iloc[2:5])
# or
print(df.iloc[3:6,])
print('select all rows starting from third row')
#print(df.iloc[2:])
# or
print(df.iloc[2:,])
#Select column by using column number in pandas with .iloc
print('Select first 2 columns: ')
print(df.iloc[:,:2])
print()
print('Select first and fourth columns: ')
print(df.iloc[:,[0,3]])
print()
#Select value by using row name and column name in pandas with .loc:
print('select value by row label and column label using loc')
print(df.loc[[1,2,3,4,5],['Name','Score']])