Files Required to Load: Question#5 : titanic.csv
Question#1:
Write a Python program to match a string that contains only upper and lowercase letters, numbers, and underscores.
In [0]:
import re
def text_match(text):
patterns = ‘^[a-zA-Z0-9_]*$’
#TO DO — Complete the Code
print(“text#1: ” + text_match(“The quick brown fox jumps over the lazy dog.”))
print(“text#2: ” + text_match(“Python_Exercises_1”))
text#1: No match found!
text#2: Found a match!
Question#2:
Write a Python program to remove all whitespaces from a string.
In [0]:
import re
text1 = ‘ Python Exercises ‘
print(“Original string:”,text1)
print(“Without extra spaces:”) #TO DO — Complete the Code
Original string: Python Exercises
Without extra spaces: PythonExercises
Question#3:
Check null values in Pandas Dataframe to return False for NaN values.
In [0]:
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
dict = {‘First Score’:[100, 90, np.nan, 95],
‘Second Score’: [30, 45, 56, np.nan],
‘Third Score’:[np.nan, 40, 80, 98]}
# creating a dataframe using dictionary
df = pd.DataFrame(dict)
#TO DO — Complete the Code
Out[12]:
First Score Second Score Third Score
0 True True False
1 True True True
2 False True True
3 True False True
Question#4:
Merge DataFrames df1 and df2 .
In [0]:
import numpy as np
import pandas as pd
df1 = pd.DataFrame({‘lkey’: [‘faa’, ‘baa’, ‘bzz’, ‘faa’],
‘value’: [2, 3, 5, 7]})
df2 = pd.DataFrame({‘rkey’: [‘faa’, ‘baa’, ‘bzz’, ‘faa’],
‘value’: [7, 8, 9, 10]})
#TO DO — Complete the Code
Out[11]:
lkey value_x rkey value_y
0 faa 2 faa 7
1 faa 2 faa 10
2 faa 7 faa 7
3 faa 7 faa 10
4 baa 3 baa 8
5 bzz 5 bzz 9
Question#5:
Write a Pandas program to create a Pivot table with multiple indexes from the data set of titanic.csv
In [1]:
import pandas as pd
import numpy as np
df = pd.read_csv(‘titanic.csv’)
result = #TO DO — Complete the Code
print(result)
Unnamed: 15 adult_male alone fare parch pclass sibsp \
sex age
female 0.75 0.0 0.0 0.0 38.5166 2 6 4
1.00 0.0 0.0 0.0 26.8750 3 6 1
2.00 0.0 0.0 0.0 259.4750 9 15 9
3.00 0.0 0.0 0.0 62.6542 3 5 4
4.00 0.0 0.0 0.0 114.1417 6 13 4
5.00 0.0 0.0 1.0 90.8708 5 11 7
6.00 0.0 0.0 0.0 64.2750 3 5 4
7.00 0.0 0.0 0.0 26.2500 2 2 0
8.00 0.0 0.0 0.0 47.3250 3 5 3
9.00 0.0 0.0 0.0 108.7958 7 12 10
10.00 0.0 0.0 0.0 24.1500 2 3 0
11.00 0.0 0.0 0.0 31.2750 2 3 4
13.00 0.0 0.0 1.0 26.7292 1 5 0
14.00 0.0 0.0 1.0 169.1667 2 9 3
14.50 0.0 0.0 0.0 14.4542 0 3 1
15.00 0.0 0.0 2.0 241.0459 1 10 1
16.00 0.0 0.0 3.0 246.2625 4 12 5
17.00 0.0 0.0 3.0 210.7833 2 12 6
18.00 0.0 0.0 4.0 697.0167 9 31 6
19.00 0.0 0.0 3.0 215.0959 2 13 3
20.00 0.0 0.0 1.0 18.4875 0 6 1
21.00 0.0 0.0 4.0 410.4333 4 16 5
22.00 0.0 0.0 7.0 444.1084 6 26 3
23.00 0.0 0.0 3.0 405.5417 2 10 4
24.00 0.0 0.0 7.0 772.1708 15 31 10
25.00 0.0 0.0 1.0 223.2500 4 11 3
26.00 0.0 0.0 3.0 136.7292 1 12 2
27.00 0.0 0.0 2.0 76.8916 3 15 2
28.00 0.0 0.0 4.0 110.9458 1 16 3
29.00 0.0 0.0 2.0 320.6208 7 16 3
… … … … … … … …
male 42.00 0.0 10.0 6.0 216.1084 1 21 3
43.00 0.0 3.0 2.0 40.7500 1 8 1
44.00 0.0 6.0 3.0 156.1250 1 15 3
45.00 0.0 6.0 5.0 187.1000 0 10 1
45.50 0.0 2.0 2.0 35.7250 0 4 0
46.00 0.0 3.0 2.0 166.3750 0 4 1
47.00 0.0 7.0 7.0 181.3583 0 12 0
48.00 0.0 5.0 3.0 176.1334 0 8 2
49.00 0.0 4.0 1.0 256.9167 1 6 3
50.00 0.0 5.0 2.0 317.0250 0 8 4
51.00 0.0 6.0 5.0 123.3084 1 13 0
52.00 0.0 4.0 3.0 136.6500 1 6 1
54.00 0.0 5.0 3.0 195.1500 1 8 1
55.00 0.0 1.0 1.0 30.5000 0 1 0
55.50 0.0 1.0 1.0 8.0500 0 3 0
56.00 0.0 3.0 3.0 92.7458 0 3 0
57.00 0.0 1.0 1.0 12.3500 0 2 0
58.00 0.0 2.0 1.0 142.9750 2 2 0
59.00 0.0 2.0 2.0 20.7500 0 5 0
60.00 0.0 3.0 1.0 144.7500 2 4 2
61.00 0.0 3.0 3.0 72.0583 0 5 0
62.00 0.0 3.0 3.0 63.6000 0 4 0
64.00 0.0 2.0 1.0 289.0000 4 2 1
65.00 0.0 3.0 2.0 96.2792 1 5 0
66.00 0.0 1.0 1.0 10.5000 0 2 0
70.00 0.0 2.0 1.0 81.5000 1 3 1
70.50 0.0 1.0 1.0 7.7500 0 3 0
71.00 0.0 2.0 2.0 84.1584 0 2 0
74.00 0.0 1.0 1.0 7.7750 0 3 0
80.00 0.0 1.0 1.0 30.0000 0 1 0
survived
sex age
female 0.75 2
1.00 2
2.00 2
3.00 1
4.00 5
5.00 4
6.00 1
7.00 1
8.00 1
9.00 0
10.00 0
11.00 0
13.00 2
14.00 3
14.50 0
15.00 4
16.00 5
17.00 5
18.00 8
19.00 7
20.00 0
21.00 4
22.00 10
23.00 4
24.00 14
25.00 2
26.00 3
27.00 5
28.00 5
29.00 5
… …
male 42.00 3
43.00 0
44.00 1
45.00 2
45.50 0
46.00 0
47.00 0
48.00 3
49.00 2
50.00 1
51.00 1
52.00 1
54.00 0
55.00 0
55.50 0
56.00 1
57.00 0
58.00 0
59.00 0
60.00 1
61.00 0
62.00 1
64.00 0
65.00 0
66.00 0
70.00 0
70.50 0
71.00 0
74.00 0
80.00 1
[145 rows x 8 columns]
Question#6:
Write a Python program to visualize the state/province wise Active cases of Novel Coronavirus (COVID-19) in USA.
In [ ]:
HW#3B Data Load Process
Files Required to Load: Question#3 : test.txt ; Question#4 : countries.csv
Question#1:
Write a Python program to convert JSON data to Python object.
In [1]:
import json
json_obj = ‘{ “Name”:”David”, “Class”:”I”, “Age”:6 }’
python_obj = #TO DO — Complete the Code
print(“\nJSON data:”)
print(python_obj)
print(“\nName: “,python_obj[“Name”])
print(“Class: “,python_obj[“Class”])
print(“Age: “,python_obj[“Age”])
JSON data:
{‘Name’: ‘David’, ‘Class’: ‘I’, ‘Age’: 6}
Name: David
Class: I
Age: 6
Question#2:
Write a Python program to convert Python object to JSON data
In [2]:
import json
# a Python object (dict):
python_obj = {
“name”: “David”,
“class”:”I”,
“age”: 6
}
print(type(python_obj))
# convert into JSON:
j_data = #TO DO — Complete the Code
# result is a JSON string:
print(j_data)
<class ‘dict’>
{“name”: “David”, “class”: “I”, “age”: 6}
Question#3:
Write a python program to find the longest words in the provided text file.
In [3]:
def longest_word(filename):
with open(filename, ‘r’) as infile:
words = #TO DO — Complete the Code
max_len = #TO DO — Complete the Code
words = infile.read().split()
print(longest_word(‘test.txt’))
[‘general-purpose,’, ‘object-oriented,’]
Question#4:
Write a Python program to read a given CSV file having tab delimiter.
In [4]:
import csv
with open(‘countries.csv’, newline=”) as csvfile:
data = #TO DO — Complete the Code
for row in data:
print(‘, ‘.join(row))
country_id country_name region_id
AR Argentina 2
AU Australia 3
BE Belgium 1
BR Brazil 2
CA Canada 2
CH Switzerland 1
CN China 3
DE Germany 1
DK Denmark 1
EG Egypt 4
FR France 1
HK HongKong 3
IL Israel 4
IN India 3
IT Italy 1
JP Japan 3
KW Kuwait 4
MX Mexico 2
NG Nigeria 4
N Netherlands 1
SG Singapore 3
UK United Kingdom 1
US United States of America 2
ZM Zambia 4
ZW Zimbabwe 4
Question#5:
Write a Python program to create a SQLite database and connect with the database and print the version of the SQLite database.
In [14]:
import sqlite3
try:
sqlite_Connection = sqlite3.connect(‘temp.db’)
conn = sqlite_Connection.cursor()
print(“\nDatabase created and connected to SQLite.”)
sqlite_select_Query = “select sqlite_version();”
#TO DO — Complete the Code
record = conn.fetchall()
print(“\nSQLite Database Version is: “, record)
conn.close()
except sqlite3.Error as error:
print(“\nError while connecting to sqlite”, error)
finally:
if (sqlite_Connection):
sqlite_Connection.close()
print(“\nThe SQLite connection is closed.”)
Database created and connected to SQLite.
SQLite Database Version is: [(‘3.33.0’,)]
The SQLite connection is closed.
Question#6:
Write a Python program to create a table and insert some records in that table. Finally selects all rows from the table and display the records.
create a table CREATE TABLE salesman(salesman_id n(5), name char(30), city char(35), commission decimal(7,2));
insert some records INSERT INTO salesman VALUES(5001,’James Hoog’, ‘NY’, 0.15);
INSERT INTO salesman VALUES(5002,’Nail Knite’, ‘Paris’, 0.25);
INSERT INTO salesman VALUES(5003,’Pit Alex’, ‘London’, 0.15);
INSERT INTO salesman VALUES(5004,’Mc Lyon’, ‘Paris’, 0.35);
INSERT INTO salesman VALUES(5005,’Paul Adam’, ‘Rome’, 0.45);
display the records SELECT * FROM salesman;
In [15]:
import sqlite3
#TO DO — Complete the Code
print(“creating connecting …”)
conn = sqlite3.connect (‘mydatabase.db’ )
conn . close ()
print(“\nThe SQLite connection is closed.”)
creating connecting …
The SQLite connection is closed.