Python Data Analysis Class Booklet
Python Data Analysis Class Booklet
Data Analysis
Python
Class Booklet
T-M
General instructions:
5. Run the cell - make sure the cell appears with the title
After each cell, run the cell and check that the result is correct
8. Define a new Markdown cell and inside it, write a large heading:
Python - Lesson 1 - Exercises solution
9. Underneath that one, add a Markdown cell with the title: Numbers
10. Define a variable named my_number and insert the value 500
16. Using the variables you defined in the previous sections, calculate what amount
the customer would pay if he received a 10% discount.
Perform the assignment statement (save the value) to the variable named
total_price
18. Show the result of dividing total_price by 3 but show only a whole number
20. Define a Markdown cell with the title: String slicing and methods
21. Define a new variable named my_name, and put in your name
22. Define a new variable named city, and put in the name of the city where you live
24. Set to print (<> refers to the value in the variable in parentheses):
Welcome <my_name> from <City>, please pay <total_price>
Hello <my_name>,
It's our first Python lesson How fun! =)
26. What will be the result of the expression? Think to yourself first and only then
check the software:
"He" + "l" * 2 + "o" + " Class - " + "Python " + str(100//5) + str(45%23)
30. Print the address from the beginning to the end in increments of 3
32. Print the following text exactly as it appears (including line breaks and
indentations):
General instructions:
2. Go to the folder you opened, and create a new notebook called Ex_sol_2
(Make sure that the notebook opens in the desired location)
6. Import the data file 'hotel_bookings.csv', and save it into a variable named df.
Examine the CSV file, and if necessary use appropriate parameters so that the data
will be entered accurately
8. Show only the first 5 lines - what is the month and year in which the data begins?
9. Show only the last 5 lines - what is the month and year in which the data ends?
10. Use one or more methods to answer the following questions regarding df:
d. Check if there is a date column, and if so, is the data type set as a date?
Write an arithmetic exercise that will calculate the number of rows and columns,
and check that the result is the same
a. You can see how many columns and how many rows there are, and write the
exercise manually
b. Challenge:
How can you get a TRUE or FALSE response by only using code for this
question?
(You can use everything we learned, even in a previous lesson)
d. Are most of the vacationers at the hotel returning customers or first timers?
e. What is the average number of days one is on the waiting list in order to
reserve a room at the hotel?
16. Continuing from the previous section, use the ‘describe’ method for the column.
18. Continuing from the previous section, run an assignment and save the results in a
new variable called sdf
21. What is the minimum value and what is the maximum value of the nights reserved
in the middle of the week?
22. What is the minimum value and what is the maximum value of the nights reserved
on the weekend?
23. Filter the data and show only the row with the most nights reserved in the middle
of the week. Does this row also have the maximum number of nights on a
weekend?
24. Show only the reservations where customers have checked out.
General instructions:
2. Go to the folder you opened, and create a new notebook called Ex_sol_3
6. Import the data file 'hotel_bookings.csv', and save it in a variable named df.
Make sure you import the data properly (dates, omitting unnecessary rows). Also,
import the data in the reservation_no column so that it appears in the label index
9. Which label index contains the highest number of nights in the middle of the
week?
10. Show every label index row which contains the highest number of nights in the
middle of the week
11. Display the name of the hotel with the lowest number of reserved nights on a
weekend
13. Look at the reservation_status column, how many unique values are there in this
column?
14. Show the value which is repeated the most number of times, and the number of
times it is repeated
15. Challenge:
Run an assignment on a variable called ‘status’, on the value which is repeated
most often, by only writing code
17. What is the highest number of children in the reservations for which the customer
arrived at the hotel?
If you did the challenge in section 15, you can use the value in the status variable
a. Arrival_date_year → year
b. arrival_date_month → month
c. arrival_date_week_number → week
d. arrival_date_day_of_month → day
20. Display only the columns ‘Number of nights in the middle of the week’ and
‘Number of nights on the weekend’
22. Check your answer, and see that a new column has indeed been added at the end
of the df
23. Show descriptive statistics (describe) for the Total Nights column.
24. Sort the data by hotel and then by the total amount of nights from high to low.
25. Because the hotel looks at two groups of reservations - reservations where the
guests have arrived at the hotel, and reservations which were canceled - update all
reservations with the status No-Show to Canceled
We will now work with new data - data on cars stopped by traffic police
27. Import to the notebook the [Link] file. Pay attention to the date column
29. Add a column that contains the year, taken from the date
30. Add a column that contains the month, taken from the date
31. Check that the columns have been added correctly and that they contain the
correct information
General instructions:
2. Go to the folder you opened, and create a new notebook called Ex_sol_4
(Make sure that a notebook has been opened in the desired location)
4. Open a new cell and turn it into a Markdown cell containing the following title:
NULL values and analytics
5. Import the file called [Link] to the notebook. Note the date column
6. Delete all empty columns from the df, and determine which operation to use
How many column (s) were deleted?
7. Check how many blank values there are in the column search_type
8. Delete rows that contain NULL values in only one of the following columns:
stop_date
violation_raw
violation
Hint: The number of rows in the df can be calculated by the len () function
Did filling in the missing values make a significant change to the average?
16. What is the total monetary amount of all tickets given to drivers (ticket_amount)
17. What is the total monetary amount of tickets given to drivers aged 20-50?
19. What is the maximum age of male drivers arrested for speeding?
20. How many women have been arrested for the traffic offense Call for Service?
21. Who had more vehicle searches conducted (search_conducted), men or women?
By what percentage more?
General instructions:
2. Go to the folder you opened, and create a new notebook called Ex_sol_5
(Make sure that a notebook has been opened in the desired location)
If there is a column with a date, change the import so that the column is properly
imported.
6. Create a new column called stop_year, which will contain only the year a vehicle
was stopped by traffic police
7. Add a column called stop_month that will contain the month the vehicle was
stopped by traffic police
8. Check that the columns have been added correctly and that they contain the
correct information
9. Group the data by year, and save into a new object called g_year
Instruction:
Use g_year, and run a method on it that counts how many rows there are in each
group (year).
Sort the results in descending order, showing only the first three rows
11. Use the g_year object and show for each year:
f. What is the average number of the tickets for that year (ticket_amount)?
12. Save the result from the operation of the previous section in an object named
temp_df.
(The object will contain a different temporary df each time we want to investigate)
Preview:
14. Sort temp_df by the values in the sum column in descending order.
15. Sort temp_df by the values in the mean column in descending order.
16. Use temp_df, and calculate what is the average of the sum of the tickets of all the
months?
Hint: run an average calculation on the total amount of tickets for each month
17. Filter the data in temp_df, and display the years in which the monetary amount of
tickets was higher than the average amount of tickets (mean_years_ticket)
18. Create a new df named yearly_ticket_data, which contains the total monetary
amount of tickets given each year.
Instruction:
Use the g_year object (the data is grouped by year) to create a new df named
yearly_ticket_data, which contains the monetary sum of the tickets.
19. Add to a new df (yearly_ticket_data) a column named prev_month that will contain
the total monetary amount of tickets given in the previous month
Note that since we have a NaN value, there will still be displayed results with one
digit after the decimal.
21. Using the yearly_ticket_data, show descriptive statistics for the ticket_amount
column
22. Group the traffic ticket data for speeding violations by gender.
Is there a gender that has received significantly more speeding tickets than the
other gender?
23. Group traffic offense data by year and month and save in a variable named
g_month
24. Show the ten months (and the year in which each month is) in which there are the
most traffic offenses
(That is, create a pivot table that will have traffic offenses listed in the rows, and
gender in the columns, and the table itself will have the amount of traffic offenses)
2. Show the distribution of traffic offenses by gender for 2013 and later
(That is, a table with traffic offenses listed in the rows, while the columns will have
the last three years, where for each year there are two columns: men / women. The
data in the table will contain the number of offenses)
3. For 2013 and later, how many men / women were arrested (is_arrested) after
committing a traffic offense?
Note: You can calculate the entire df, and look only at the appropriate row in the
result
5. Create a pivot table that shows for 2013 and later the distribution of traffic
offenses for each year and gender but only for arrested drivers (is_arrested).
Save the result within a variable called temp_df
6. Use temp_df and show only the traffic offenses (rows) in which in one of the years
(2013-2015) men committed 15 or more traffic offenses
Preview:
8. Create a pivot table only for the 2015 data that shows the monthly distribution of
the number of tickets by gender.
Save the result within a variable called temp_df
9. Use temp_db and apply the cumulative sum method to display the amount of
tickets up to (and including) the month in the row
What was the amount of tickets for men and the amount for women from the
beginning of 2015 up to and including June 2015?
Use a dense ranking so that months with the same rank get the same number, and
the month in the subsequent ranking will get the number which follows
a. To make it easier for you, you can run a ranking once for the men’s data and
once for the women
b. For both genders, what is the month with the lowest number of rankings?
c. For both genders, what is the month with the highest number of rankings?
General instructions:
2. Go to the folder you opened, and create a new notebook called Ex_sol_6
(Make sure that a notebook has been opened in the desired location)
In this section we will look at the daily grocery shopping list of three families
5. From the "Data" folder, import the following files into DataFrame objects, and
display their contents:
(These files contain each family's daily shopping list)
Reminder: If you want to import from a subfolder, the file name should be:
'subfolder_name / file_name.csv'
6. Merge the shopping data of the three families (df1, df2, df3) into a new DataFrame
called shop_df, which will contain the complete shopping list of all the families.
7. Because the indices of the rows are in disarray, please re-index the rows anew.
10. What is the best-selling item, and how many times was it sold?
11. Which family purchased the most items, how many times did the family shop?
12. Which family bought the most items if you don’t include the sales for items 1000 &
1011.
2. From the "Data" folder, import the Groceries file into a variable named gr_df
3. Merge the information from gr_df and shop_df into a new variable called sales, so
that for each row in shop_df, the product description and price will also be added.
• Customer number
• Product Code
• Description
• Price
7. What is the amount of the most expensive item purchased by family number 2?
9. Challenge question:
What item/s has/have been purchased the most times?
10. Use Group and Aggregate of the data to display a "Product Sales Report" that will
contain the following columns:
• Product code
• Quantity sold
Preview:
We will now look at the products in the product table, in order to understand
which are the best-selling products and for which products there is no demand
12. Merge the information from gr_df and shop_df into a new variable called
products, so that for each row in gr_df, the product's sales data will be added - if
there were any (that is, if there were no sales for the product, the product data will
appear without sales data, and if the product had more than one sale, a row will
appear for each sales row)
14. Use Group and Aggregate of the data to display a "Product Sales Report" that will
contain the following columns:
• Product code
• Quantity sold
Preview:
15. Continuing from the previous section, what is the difference between this report
and the report in section 10?
General instructions:
2. Go to the folder you opened, and create a new notebook called Ex_sol_7
(Make sure that a notebook has been opened in the desired location)
If there is a column with a date, adjust the import so that the column is imported
correctly.
6. Create a new column called stop_year, which will contain only the year the vehicle
was stopped by the police.
7. Add a column called stop_month which will contain the month the vehicle was
stopped by the police.
8. Add a column called stop_day_of_week which will contain the day of the week the
vehicle was stopped by the police.
9. View the first three rows of data from the df, and check that the columns you
added have been added correctly
12. Continuing from the previous question, change the answer you got from the data
into a column chart type.
Add a title to the chart, and change the colors of the bars
13. Continuing from the previous question, copy the code you wrote down into a new
cell, and add to the display a new series of the 2015 data.
The series should be of the line chart type, with markers in the shape you choose
and a line in black
Sample Preview:
14. Based on the chart from the previous question, answer the following questions:
a. On which day are there the most vehicle stops by the traffic police?
b. Examine and compare the data for 2010 and 2015 - has the situation on the
roads improved?
Express your opinion. Would you need to continue researching the data in
order to express a more accurate opinion?
If so, list the questions you would like to explore (just list questions, do not
solve them)
Create a pivot table that shows for each year the distribution of traffic offenses
(rows) classified as Speeding by Gender (columns)
16. Continuing from the previous question, create a Stacked bar chart which shows
the distribution of the speeding offenses over the years by gender.
17. Create a pie chart that shows the distribution percentage of the number of times a
vehicle was searched (search_conducted column) compared to the number of
times that a search was not conducted
Hint: value_counts
Preview example:
Part 2 - Seaborn
20. Copy the following code to the notebook, and run it:
[Link](figsize=(15,5))
[Link](data=df, x='driver_age', y='ticket_amount', hue='driver_gender')
21. Use countplot to create a chart showing the distribution of traffic offenses
(violation column) by type of traffic offense.
(Note that the order of the rows is important ... If the display does not
change, try changing the position of the line of code that defines the size of
the chart)
b. Add a title to the chart, and set the font size of the title to 20
22. Continuing from the previous question, add the following line to the end of the
code of the chart ... What has changed?
Hint 1:
Parameter hue
Hint 2:
In order for tags with the data values to also appear in the additional series, add
the following line, which defines the code to add the tags to the second series as
well:
plt.bar_label([Link][1],size=12)
Preview example:
General instructions:
2. Go to the folder you opened, and create a new notebook called Ex_sol_8
(Make sure that a notebook has been opened in the desired location)
7. Set a variable named conn, for the SQL server connection type, and set the
connection properties for your computer.
Note - it is necessary to define the query and the connection properties. Use this
picture for help:
10. View the contents of the ppl_names variable (that is, the results of the query
above)
11. Just looking at what was printed in the previous section - how many records did
you get?
12. Show how many times each of the first names are repeated
13. Continuing from the previous section, show only the 10 most common first names,
and the number of occurrences of each
14. Go back to the MSSQL Management Studio, and write a query that shows the
2013 data only, as detailed below.
Show data:
d. Product ID (ProductID)
h. Product color
15. Enter the results of the query into a variable called sales
16. View the query results you received, and check that the number of rows is the
same as the number of rows you got when running MSSQL Management Studio
17. Group sales data by color (Color), and for each color display the number of
orders, the number of items ordered, and the total number of orders.
18. Continuing from the previous section, sort the data by total orders from highest to
lowest.
Hint: Pivot_table
22. The following libraries will be used to work with charts and integration with Excel:
c. import openpyxl
e. %matplotlib inline
23. Import from the seaborn library the dataset called taxis into the df variable, by
running the following line of code:
df = sns.load_dataset ('taxis')
27. Activate the worksheet that is created automatically when you create a new
workbook
29. Add in the active sheet all the content contained in the df variable
Note that there are two ways to put information into a worksheet. One is suitable
for a processed df (for example, a pivot table), and another way is for raw df data
Also, make sure that you include the column headings in the worksheet
30. Change the tab color (tabColor) of the worksheet to black (000000)
31. Add password protection to this worksheet so that the data can not be modified.
Password: 1234
34. Create a pivot table that shows for each borough (pickup_borough) how many
pick-ups there were for each color of cab (color). Save the pivot table inside a
variable called piv
36. Create from the variable piv a horizontal bar chart, add a suitable title, and color
the series in the chart according to the color of the taxi
40. Add the contents of the pivot table to the active worksheet
General instructions:
2. Go to the folder you opened, and create a new notebook called Ex_sol_9
(Make sure that a notebook has been opened in the desired location)
4. Manually calculate the following (without copying and running this in the jupyter
notebook), what is the answer for this code snippet:
x=2
if x == 3:
print ("x is 3")
elif x < 3:
print ("x is less than 3")
else:
print ("x is more than 3")
5. Define a variable named my_num, and update it with any number you choose
6. Define a variable named chosen_food, and insert your favorite food (insert = place
a value into the variable )
7. Write a conditional block that will check the value in the chosen_food variable:
b. If the name of the dish is 5-10 characters in total, print: "Let's eat!"
a. If your full name is up to 10 characters (but not including) - Print your full
name from the end to the beginning (backwards)
c. If your full name is longer than 14 characters, print your name by skipping
every other character (that is, the first character will be printed, the next
character will not be printed)
10. Open a new cell and turn it into a Markdown cell, and place the following main
header: Loops
for example:
T
A
L
I
A
M
O
R
C
H
I
13. The Fibonacci series is the series whose first two numbers are 0, 1 and each
number thereafter is equal to the sum of its two predecessors. Therefore, the first
several numbers of the series are:
0, 1, 1, 2, 3, 5, 8, 13, 21…
Write a code that goes uses the numbers from 0 to 50, and produces a Fibonacci
series
15. Create a function called multiple, which receives a list, and doubles all the items in
the list
16. Create a function that receives a list and returns a list with only the even numbers
20. Check if the function is working properly by calling to the function and entering
appropriate arguments (e.g. 5, 18, 70)
21. Add a new column to the table called age_category, which will contain the age
category of the person who was stopped, by running the get_age_category
function on the driver_age column
Hint: apply
22. Calculate how many traffic offenses are associated with each age category
(age_category)
23. Continuing from the previous section, create a bar chart that shows the
distribution of traffic offenses by age category (age_category).
24. Continuing from the previous section, what is the age category (age_category) in
which most traffic offenses were committed?
25. Define a user function called get_violation_severity, which accepts the traffic
offense committed (violation), and returns its severity according to the following
categories:
26. Check if the function is working properly by calling to the function and sending
appropriate arguments (for example 'Speeding', 'Equipment', 'Moving violation')
27. Add a new column to the table called violation_severity, which will contain the
severity category of the traffic offense by running the get_violation_severity
function on the violation column
Hint: apply
28. Create a pie chart that shows the distribution of traffic offenses according to the
severity category of the traffic offense (violation_severity).
Note that the chart must be sorted by traffic offense severity category
(violation_severity)
29. Continuing from the previous section, what is the category of traffic offense
severity (violation_severity) in which the most traffic offenses were committed?
d. To check this, call to the function you created and check your result:
split ('[Link].!.!.!', '.')
31. Create a function that receives a list of numbers and returns True if all the numbers
are different from each other, otherwise it returns False