Lecture: Learning Python 2-Sample Data ver. 4

Last Updated on August 8, 2022 by shibatau

IV is added.

I. What will you learn?

These posts are Python tutorials for beginners and used for my lectures.

Learning Python 0-Cheat Sheets

Learning Python 1-Google Colaboratory

Learning Python 2-Sample data

Learning Python 3-Lists and Comprehensions

Learning Python 4-Dictionaries and Data frames

Learning Python 5-Functions

Learning Python 6-If Statements

Learning Python 7-For Loops

Learning Python 8-Classes and Objects

II. The three datasets for learning

These are the sample datasets for the lectures.

1.HTML tables

These  are sample datasets for learning Python with Cheat Sheets.

Table 1

id english japanese nationality department classes gender
1 17.8 75.6 japan literature 2 male
2 64.4 53.3 nepal literature 2 male
3 86.7 31.1 nepal literature 1 male
4 60 62.2 indonesia literature 2 male
5 42.2 80 japan literature 1 male
6 33.3 75.6 japan literature 1 male
7 28.9 60 japan literature 2 male
8 53.3 88.9 japan literature 1 male
9 42.2 60 japan literature 1 male
10 40 80 japan literature 1 male
11 33.3 82.2 japan literature 2 female
12 24.4 75.6 china economics 2 male
13 57.8 80 japan literature 1 male
14 62.2 86.7 japan literature 2 male
15 62.2 71.1 japan literature 2 male
16 62.2 86.7 japan literature 1 male
17 40 68.9 japan literature 1 male
18 84.4 57.8 nepal literature 1 male
19 73.3 84.4 china literature 2 female
20 28.9 55.6 japan economics 2 male
21 64.4 31.1 nepal literature 2 male
22 71.1 75.6 japan literature 2 female
23 37.8 51.1 vietnam literature 1 female
24 33.3 40 vietnam literature 2 male
25 84.4 33.3 nepal literature 2 male
26 77.8 77.8 nepal literature 1 male
27 35.6 73.3 japan literature 2 female
28 31.1 80 japan literature 1 male
29 24.4 64.4 japan economics 1 male
30 26.7 53.3 japan literature 2 male
31 66.7 28.9 nepal literature 1 male
32 26.7 66.7 japan economics 1 female
33 53.3 82.2 japan literature 2 male
34 26.7 60 china literature 2 male
35 62.2 82.2 japan literature 2 male
36 26.7 80 japan economics 1 male
37 71.1 60 vietnam literature 2 male
38 33.3 31.1 indonesia literature 1 male
39 51.1 75.6 japan literature 2 female
40 53.3 88.9 japan literature 2 male
41 24.4 60 japan economics 2 male
42 31.1 42.2 vietnam literature 1 male
43 33.3 40 vietnam literature 1 female
44 46.7 77.8 japan literature 1 female
45 80 57.8 nepal literature 2 male
46 93.3 55.6 nepal literature 2 male
47 60 80 japan literature 2 male
48 55.6 75.6 japan literature 1 male
49 35.6 91.1 japan literature 2 male
50 51.1 71.1 japan literature 1 male
51 26.7 60 japan literature 2 male
52 24.4 68.9 japan literature 2 male
53 53.3 84.4 japan literature 2 male
54 62.2 82.2 japan literature 2 male
55 64.4 82.2 japan literature 2 female
56 31.1 64.4 japan literature 2 male
57 37.8 68.9 china literature 2 male
58 33.3 62.2 japan literature 2 male
59 57.8 44.4 vietnam literature 2 female
60 82.2 77.8 nepal literature 2 male
61 40 80 china literature 2 male
62 60 26.7 vietnam literature 1 male
63 28.9 42.2 vietnam literature 1 male
64 51.1 68.9 japan literature 1 female
65 33.3 64.4 japan economics 2 male
66 55.6 80 japan economics 1 male
67 80 37.8 nepal economics 2 male
68 35.6 35.6 nepal economics 1 male
69 28.9 73.3 japan economics 2 male
70 28.9 57.8 china economics 2 female
71 35.6 53.3 japan economics 2 male
72 15.6 84.4 japan economics 2 male
73 26.7 64.4 japan economics 1 male
74 28.9 62.2 japan economics 1 male
75 15.6 60 japan economics 2 male
76 37.8 71.1 japan economics 2 male
77 31.1 42.2 japan economics 2 male
78 46.7 75.6 japan economics 1 male
79 31.1 71.1 japan economics 2 male
80 35.6 68.9 japan economics 1 male
81 22.2 55.6 japan economics 1 male
82 15.6 75.6 japan economics 1 male
83 20 71.1 japan economics 2 male
84 28.9 48.9 japan economics 2 male
85 48.9 64.4 japan economics 1 male
86 26.7 71.1 japan economics 2 male
87 31.1 28.9 japan economics 2 male
88 22.2 60 japan economics 1 male
89 64.4 86.7 japan economics 1 female
90 24.4 75.6 japan economics 2 male
91 20 51.1 japan economics 2 male
92 33.3 51.1 japan economics 1 male
93 53.3 86.7 japan economics 2 male
94 88.9 33.3 nepal economics 2 male
95 20 80 japan economics 2 male
96 28.9 31.1 japan economics 1 male
97 28.9 42.2 japan economics 2 male
98 48.9 40 nepal economics 2 male
99 44.4 55.6 japan economics 2 male
100 28.9 57.8 vietnam economics 2 female
101 24.4 60 japan economics 1 male
102 55.6 66.7 vietnam economics 2 female
103 62.2 55.6 vietnam economics 2 female
104 35.6 35.6 vietnam economics 2 male
105 51.1 53.3 vietnam economics 2 male
106 46.7 44.4 vietnam economics 2 male
107 46.7 71.1 japan economics 1 female
108 28.9 75.6 japan economics 1 male
109 37.8 77.8 china economics 1 male
110 48.9 86.7 japan economics 2 female
111 46.7 73.3 japan economics 2 male
112 26.7 68.9 japan economics 1 male
113 46.7 73.3 japan economics 2 male
114 42.2 57.8 japan economics 1 male
115 55.6 62.2 japan economics 2 male
116 24.4 86.7 japan economics 1 male
117 31.1 73.3 japan economics 2 male
118 42.2 68.9 japan economics 2 male
119 86.7 64.4 nepal economics 1 male
120 42.2 24.4 nepal economics 1 male
121 48.9 55.6 china economics 2 male
122 31.1 66.7 china economics 2 male
123 31.1 68.9 japan economics 2 male
124 46.7 80 japan economics 1 male
125 53.3 71.1 japan economics 1 male
126 51.1 88.9 japan economics 2 female
127 37.8 73.3 vietnam economics 2 female
128 35.6 82.2 japan economics 2 male
129 22.2 66.7 japan economics 1 male
130 35.6 48.9 japan economics 1 male
131 40 73.3 japan economics 1 male
132 71.1 84.4 japan economics 2 female
133 64.4 53.3 nepal economics 2 male
134 80 53.3 nepal economics 1 male
135 75.6 68.9 nepal economics 2 male
136 31.1 71.1 vietnam economics 2 male
137 26.7 73.3 japan economics 1 male
138 22.2 40 vietnam economics 2 male
139 73.3 77.8 vietnam economics 2 female
140 22.2 44.4 vietnam economics 1 male
141 86.7 80 china economics 2 male
142 40 46.7 japan economics 1 male
143 26.7 53.3 japan economics 2 male
144 62.2 55.6 vietnam economics 1 female
145 37.8 77.8 china economics 2 female
146 33.3 55.6 japan economics 1 male
147 62.2 77.8 japan economics 1 male
148 42.2 75.6 japan economics 1 male
149 20 62.2 japan economics 2 male
150 24.4 60 japan economics 1 male
151 22.2 68.9 japan economics 2 male
152 37.8 57.8 japan economics 2 male
153 33.3 73.3 japan economics 2 female
154 33.3 64.4 japan economics 2 male
155 60 57.8 indonesia economics 1 male
156 24.4 44.4 japan economics 1 male
157 60 80 japan economics 2 male
158 31.1 66.7 japan economics 2 male
159 35.6 64.4 japan economics 1 female
160 28.9 60 japan economics 2 male
161 57.8 60 nepal economics 2 male
162 48.9 60 nepal economics 2 male
163 42.2 80 japan economics 2 female
164 20 51.1 japan economics 1 male
165 48.9 82.2 japan economics 2 male
166 28.9 60 japan economics 1 male
167 22.2 42.2 japan economics 2 male
168 40 91.1 vietnam economics 2 female
169 33.3 71.1 vietnam economics 2 male
170 31.1 33.3 japan economics 1 male
171 35.6 64.4 japan economics 1 male
172 44.4 20 nepal economics 1 male
173 60 22.2 nepal economics 1 male
174 42.2 40 japan economics 2 male
175 31.1 64.4 japan economics 1 male
176 20 62.2 japan economics 1 male
177 42.2 88.9 japan economics 1 male
178 24.4 48.9 japan economics 1 male
179 46.7 91.1 japan economics 2 male
180 33.3 64.4 japan economics 2 male
181 26.7 44.4 japan economics 2 male
182 20 60 japan economics 1 male
183 40 62.2 japan economics 1 male
184 40 57.8 japan economics 2 female
185 53.3 28.9 nepal economics 1 male
186 33.3 84.4 japan economics 1 male
187 24.4 51.1 japan economics 1 male
188 42.2 53.3 japan economics 1 male
189 44.4 88.9 japan economics 2 female
190 40 80 japan economics 2 female
191 17.8 53.3 japan economics 2 male
192 48.9 82.2 japan economics 2 male
193 28.9 75.6 japan economics 1 male
194 37.8 73.3 japan economics 1 male
195 84.4 28.9 nepal economics 2 male
196 66.7 55.6 nepal economics 2 male
197 44.4 80 china economics 2 male
198 57.8 48.9 vietnam economics 1 female
199 86.7 26.7 vietnam economics 2 male
200 24.4 68.9 japan economics 2 male

Table 2

id classes att1 att2 att3 att4 att5 att6 att7 att8 att9 att10 att11 att12 att13 att14 att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1 1 1
58 cew2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
59 cew2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
60 cew2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
61 cew2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
62 cew2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
63 cew2 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1
64 cew2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
65 cew2 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1
66 cew2 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1
67 cew2 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1
68 cew2 1 1 1 1 0 0 1 1 0 1 0 1 1 1 1
69 cew2 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
70 cew2 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1
71 cew2 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1
72 cew2 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1

Table 3

id classes res1 res2 res3 res4 res5 res6 res7
1 bew1 strongly agree strongly agree moderate strongly agree strongly agree strongly agree strongly agree
2 bew1 strongly agree strongly agree moderate strongly agree strongly agree strongly agree strongly agree
3 bew1 strongly agree strongly agree moderate strongly agree strongly agree strongly agree strongly agree
4 bew1 strongly agree strongly agree moderate agree strongly agree agree strongly agree
5 bew1 strongly agree agree moderate strongly agree strongly agree agree strongly agree
6 bew1 agree agree easy agree agree agree agree
7 bew1 agree agree moderate agree agree agree agree
8 bew1 agree agree moderate agree agree agree agree
9 bew1 strongly agree strongly agree very difficult strongly agree strongly agree strongly agree strongly agree
10 bew1 strongly agree strongly agree moderate strongly agree strongly agree strongly agree agree
11 bew1 strongly agree strongly agree moderate strongly agree strongly agree strongly agree strongly agree
12 bew1 strongly agree strongly agree difficult strongly agree agree strongly agree strongly agree
13 bew1 strongly agree strongly agree difficult strongly agree strongly agree strongly agree strongly agree
14 bew1 strongly agree strongly agree difficult strongly agree strongly agree strongly agree strongly agree
15 bew1 strongly agree agree difficult strongly agree agree agree strongly agree
16 bew1 strongly agree agree difficult agree agree strongly agree strongly agree
17 bew1 strongly agree strongly agree difficult strongly agree strongly agree strongly agree strongly agree
18 bew1 agree agree moderate neutral agree agree agree
19 bew1 strongly agree strongly agree difficult agree agree strongly agree agree
20 bew1 strongly agree strongly agree difficult agree agree strongly agree agree
21 bef4 strongly agree strongly agree easy strongly agree strongly agree strongly agree strongly agree
22 bef4 agree agree easy neutral agree agree strongly agree
23 bef4 strongly agree strongly agree very easy strongly agree agree strongly agree strongly agree
24 bef4 strongly agree strongly agree easy strongly agree strongly agree strongly agree agree
25 bef4 agree neutral moderate agree neutral neutral agree
26 bef4 strongly agree agree moderate strongly agree strongly agree strongly agree strongly agree
27 bef4 strongly agree strongly agree difficult strongly agree strongly agree strongly agree strongly agree
28 bef4 strongly agree strongly agree difficult agree strongly agree strongly agree strongly agree
29 bef4 strongly agree strongly agree difficult agree agree strongly agree strongly agree
30 bef4 strongly agree strongly agree moderate strongly agree strongly agree strongly agree strongly agree
31 bef4 agree disagree moderate agree disagree strongly agree strongly agree
32 bef4 agree agree difficult neutral neutral agree strongly agree
33 bef4 agree agree very difficult agree agree agree agree
34 bef4 disagree agree very difficult neutral strongly agree strongly agree strongly agree
35 bef4 agree agree difficult neutral agree agree agree
36 bef4 agree neutral easy agree agree agree strongly agree
37 bef4 agree agree moderate neutral neutral agree agree
38 bef4 strongly agree agree moderate agree agree agree agree
39 bef4 agree strongly agree moderate neutral agree disagree neutral
40 bef4 strongly agree strongly agree moderate strongly agree strongly agree strongly agree strongly agree

2.CSV raw data

These are the links to the CSV raw data pages. The data are the same as the data above.

Table 1

https://pastebin.com/raw/cSZ8pYWh

Table 2

https://pastebin.com/raw/19gskrJK

Table 3

https://pastebin.com/raw/DiqmYyz3

III. How to retrieve tables using Pandas

1.Reading HTML tables with Pandas

You can read the tables with Pandas and show the first table with the following codes:

import panda as pd
df = pd.read_html('http://www.mishou.be/2021/10/04/pythonr-sample-data-for-data-analysis/')
df[0]

You can see the scripts here:

https://colab.research.google.com/drive/1etXZ-2-RVrCuc3fD6L-8HS6HeeUVQrzS?usp=sharing

2.Reading CSV raw data with Pandas

You can read CSV raw data with the following codes:

import pandas as pd
df = pd.read_csv('https://pastebin.com/raw/cSZ8pYWh')
df.head()

You can see the scripts here:

https://colab.research.google.com/drive/1nzk_YaALD52ASJe0FyOKFuEj-2zHNhbb?usp=sharing

IV. How to retrieve tables using Classes and Objects

You can also use Classes and Objects to handle CSV data.

Loading the data:

# load data from url
import csv
import requests

CSV_URL = 'https://pastebin.com/raw/cSZ8pYWh'

with requests.Session() as s:
    download = s.get(CSV_URL)

    decoded_content = download.content.decode('utf-8')

    cr = csv.reader(decoded_content.splitlines(), delimiter=',')
    
    my_list = list(cr)
    for row in my_list:
        print(row)
# remove the header
my_list.pop(0)

Creating a class:

# create a class
class Student:
    
    def __init__(self, number, english, japanese, nationality, department, classes, gender):
      self.number = number
      self.english = english
      self.japanese = japanese
      self.nationality = nationality
      self.department = department
      self.classes = classes
      self.gender = gender

    def average(self):
        return (round((self.english + self.japanese)/2, 2))

    def report(self):
        print(f"Your number: {self.number}, English: {self.english}, Japanese: {self.japanese}, Average: {self.average()}")

Creating a list of objects:

# create a list of objecst
student_list = []
for id, english, japanese, nationality, department, classes, gender in my_list:
   # covert types
   id = int(id)
   english = float(english)
   japanese = float(japanese)
   nationality = str(nationality)
   department = str(department)
   classes = str(classes)
   gender = str(gender)
   # create Student instances and append them to a list
   student_list.append(Student(id, english, japanese, nationality, department, classes, gender))

You can see all the scripts here:

https://colab.research.google.com/drive/1B84Ykq1FfuNZjl_znLgoXzjz4QRUkfPr?usp=sharing

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.