Repository navigation
Expand file tree
/
Copy pathindexing_selecting_assigning.py
More file actions
62 lines (46 loc) · 2.01 KB
/
Copy pathindexing_selecting_assigning.py
File metadata and controls
62 lines (46 loc) · 2.01 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
import pandas as pd
import os
def read_csv(file_name):
file = os.path.join('Data', file_name)
table = pd.read_csv(file, index_col=0)
return table
def indexing(table):
pd.set_option('display.max_columns', None)
# print(table.head())
print(table.country)
print(table['country']) # table['country'] == table.country
print(table['country'][0])
def index_based_selection(table):
print(table.iloc[0]) # iloc - select by index
print(table.iloc[0, 0]) # [row, column]
print(table.iloc[:, 0]) # two columns: index col and country col
print(table.iloc[0:3, 0]) # first three columns (slicing)
def label_bases_selection(table):
print(table.loc[:, 'country']) # loc - select by label
print(table.loc[0, 'country']) # [row, column]
# print(table.loc[:, 'country']) # two columns: index col and country col
print(table.loc[:, 'country':'designation']) # first three columns (slicing)
print(table.loc[:, ['country', 'price', 'title']])
def manipulating_the_index(table):
new_table = table.set_index("title")
print(new_table.head())
new_table = new_table.reset_index()
print(new_table)
def conditional_selection(table):
print(table.country == 'Italy') # True/False
print(table.loc[table.country == 'Italy']) # print if Italy True
print(table.loc[(table.country == 'Italy') & (table.points > 90)]) # 2 conditions with symbol &(and)
print(table.loc[(table.country == 'Italy') & (table.points > 90)]) # 2 conditions with symbol |(or)
print(table.loc[table.country.isin(['Italy', 'France'])]) # print if country is Italy or France
print(table.loc[table.price.notnull()]) # print if not empty (NaN)
def assigning_data(table):
table['country'] = 'Russia'
print(table)
if __name__ == '__main__':
table = read_csv('winemag-data-130k-v2.csv')
# indexing(table)
# index_based_selection(table)
# label_bases_selection(table)
# manipulating_the_index(table)
# conditional_selection(table)
assigning_data(table)