# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <b>Assign value to a variable</b>

a = 1
b = 2

# <b>Print sum of two variables (a and b)</b>

a_b_sum = a + b
print(a_b_sum)

# <b>Import pandas library</b>

import pandas as pd

# <b>Create dataframe</b>

Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4', 'Audi A5'],
        'Price': [22000,25000,27000,35000,40000],
        'Vat': [0.23, 0.23, 0.23, 0.23, 0.23]
       }
df = pd.DataFrame(Cars, columns = ['Brand', 'Price'])

# <b>Use info() method on a dataframe</b>

df.info()

# <b>Count all records in a dataframe</b>

len(df.index)

# <b>Display first four records in a dataframe</b>

df[: 4]

# <b>Calculate mean value of a Price column values</b>

df['Price'].mean()

# <b>Calculate median value of a Price column values</b>

df['Price'].median()

# <b>Find maximum value in a Price column values</b>

df['Price'].max()

# <b>Find minimum value in a Price column values</b>

df['Price'].min()

# <b>Alternative way to calculate mean, median, maximum and minimum value in a whole dataset (as well as number of records, standard deviation, first and third quartile)</b>

df['Price'].describe()

# <b>Import matplotlib library</b>

import matplotlib.pyplot as plt

# <b>Generate histogram of values in Price column (the <i>'ec'</i> param was set to specify the color of the edges)</b>

plt.hist(df['Price'], bins=[20000, 25000, 30000, 35000, 40000], ec='black')
plt.title('Histogram of car prices')
plt.xlabel('Price')
plt.ylabel('Number of cars')

plt.hist(df['Price'], bins=[20000, 22500, 25000, 27500, 30000, 32500, 35000, 37500, 40000], ec='black')
plt.title('Histogram of car prices')
plt.xlabel('Price')
plt.ylabel('Number of cars')

# <b>Create new column total_price equal to sum of value in Price column and multiplication of value in Price and Vat columns</b>

df['total_price'] = df['Price'] + df['Price'] * Cars['Vat']
df

# <b>Create a new dataframe containing data about cars more expensive than 27000</b>

expensive_cars = df.loc[df['Price'] > 27000]
expensive_cars

total_expensive_cars = df.loc[df['total_price'] > 27000]
total_expensive_cars

# <b>Import NumPy library for random values generation</b>

import numpy as np

# <b>Select two sets of random records from a dataframe</b>

np.random.seed(168)
first_random_dataframe = pd.DataFrame({'Brand': np.random.choice(df['Brand'], size=100), 'Price': np.random.randint(18000, 50000, size=100)})
second_random_dataframe = pd.DataFrame({'Brand': np.random.choice(df['Brand'], size=100), 'Price': np.random.randint(18000, 50000, size=100)})

first_random_dataframe

second_random_dataframe

# <b>Create histograms for both of random dataframes</b>

plt.hist(first_random_dataframe['Price'], bins=[18000, 20000, 22500, 25000, 27500, 30000, 32500, 35000, 37500, 40000, 42500, 45000, 47500, 50000], ec='black')
fig_size = plt.rcParams["figure.figsize"]
fig_size[0] = 8
fig_size[1] = 8
plt.title('Histogram of car prices')
plt.xlabel('Price')
plt.ylabel('Number of cars')

plt.hist(second_random_dataframe['Price'], bins=[18000, 20000, 22500, 25000, 27500, 30000, 32500, 35000, 37500, 40000, 42500, 45000, 47500, 50000], ec='black')
fig_size = plt.rcParams["figure.figsize"]
fig_size[0] = 8
fig_size[1] = 8
plt.title('Histogram of car prices')
plt.xlabel('Price')
plt.ylabel('Number of cars')

# <b>Create box plots for both of random dataframes</b>

plt.boxplot(first_random_dataframe['Price'])
plt.title('Boxplot of car prices')
plt.xlabel('Number of a dataframe')
plt.ylabel('Car price')

plt.boxplot(second_random_dataframe['Price'])
plt.title('Boxplot of car prices')
plt.xlabel('Number of a dataframe')
plt.ylabel('Car price')

StarcoderdataJupyterScriptsDedupFiltered