<jupyter_start><jupyter_text>Pandas PracticeThis notebook is dedicated to practicing different tasks with pandas. The solutions are available in a solutions notebook, however, you should always try to figure them out yourself first.It should be noted there may be more than one different way to answer a question or complete an exercise.Exercises are based off (and directly taken from) the quick introduction to pandas notebook.Different tasks will be detailed by comments or text.For further reference and resources, it's advised to check out the [pandas documnetation](https://pandas.pydata.org/pandas-docs/stable/).<jupyter_code># Import pandas
import pandas as pd
# Create a series of three different colours
colours = pd.Series(["Red","Blue","Green"])
# View the series of different colours
colours
# Create a series of three different car types and view it
car_types = pd.Series(["petrol","diesel","electric"])
car_types
# Combine the Series of cars and colours into a DataFrame
cars = pd.DataFrame({"Colour":colours,"Car Type":car_types})
cars
# Import "../data/car-sales.csv" and turn it into a DataFrame
car_records = pd.read_csv("car-sales.csv")<jupyter_output><empty_output><jupyter_text>**Note:** Since you've imported `../data/car-sales.csv` as a DataFrame, we'll now refer to this DataFrame as 'the car sales DataFrame'.<jupyter_code># Export the DataFrame you created to a .csv file
car_records.to_csv("car_records.csv")
# Find the different datatypes of the car data DataFrame
car_records.dtypes
# Describe your current car sales DataFrame using describe()
car_records.describe()
# Get information about your DataFrame using info()
car_records.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 5 columns):
 #   Column         Non-Null Count  Dtype 
---  ------         --------------  ----- 
 0   Make           10 non-null     object
 1   Colour         10 non-null     object
 2   Odometer (KM)  10 non-null     int64 
 3   Doors          10 non-null     int64 
 4   Price          10 non-null     object
dtypes: int64(2), object(3)
memory usage: 528.0+ bytes<jupyter_text>What does it show you?<jupyter_code># Create a Series of different numbers and find the mean of them
num_Series = pd.Series([2,4,3,5,6,7,9])
num_Series.mean()
# Create a Series of different numbers and find the sum of them
num_Series.sum()
# List out all the column names of the car sales DataFrame
car_records.columns
# Find the length of the car sales DataFrame
len(car_records)
# Show the first 5 rows of the car sales DataFrame
car_records.head()
# Show the first 7 rows of the car sales DataFrame
car_records.head(7)
# Show the bottom 5 rows of the car sales DataFrame
car_records.tail()
# Use .loc to select the row at index 3 of the car sales DataFrame
car_records.loc[3]
# Use .iloc to select the row at position 3 of the car sales DataFrame
car_records.iloc[3]<jupyter_output><empty_output><jupyter_text>Notice how they're the same? Why do you think this is? Check the pandas documentation for [.loc](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html) and [.iloc](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html). Think about a different situation each could be used for and try them out.<jupyter_code># Select the "Odometer (KM)" column from the car sales DataFrame
car_records["Odometer (KM)"]
# Find the mean of the "Odometer (KM)" column in the car sales DataFrame
car_records["Odometer (KM)"].mean()
# Select the rows with over 100,000 kilometers on the Odometer
car_records[car_records["Odometer (KM)"]>100000]
# Create a crosstab of the Make and Doors columns
pd.crosstab(car_records["Make"],car_records["Doors"])
# Group columns of the car sales DataFrame by the Make column and find the average
car_records.groupby("Make").mean()
# Import Matplotlib and create a plot of the Odometer column
# Don't forget to use %matplotlib inline
%matplotlib inline
import matplotlib.pyplot as plt
# Create a histogram of the Odometer column using hist()
car_records["Odometer (KM)"].hist()
# Try to plot the Price column using plot()
car_records["Price"].plot()<jupyter_output><empty_output><jupyter_text>Why didn't it work? Can you think of a solution?You might want to search for "how to convert a pandas string columb to numbers".And if you're still stuck, check out this [Stack Overflow question and answer on turning a price column into integers](https://stackoverflow.com/questions/44469313/price-column-object-to-int-in-pandas).See how you can provide the example code there to the problem here.<jupyter_code># Remove the punctuation from price column
car_records["Price"] = car_records["Price"].str.replace("[\,\$\.]","")
# Check the changes to the price column
car_records["Price"]
# Remove the two extra zeros at the end of the price column
car_records["Price"] = car_records["Price"].str[:-2]
# Check the changes to the Price column
car_records["Price"]
# Change the datatype of the Price column to integers
car_records["Price"] = car_records["Price"].astype(int)
# Lower the strings of the Make column
car_records["Make"].str.lower()<jupyter_output><empty_output><jupyter_text>If you check the car sales DataFrame, you'll notice the Make column hasn't been lowered.How could you make these changes permanent?Try it out.<jupyter_code># Make lowering the case of the Make column permanent
car_records["Make"] = car_records["Make"].str.lower()
# Check the car sales DataFrame
car_records<jupyter_output><empty_output><jupyter_text>Notice how the Make column stays lowered after reassigning.Now let's deal with missing data.<jupyter_code># Import the car sales DataFrame with missing data ("../data/car-sales-missing-data.csv")
car_records_missing = pd.read_csv("car-sales-missing-data.csv")

# Check out the new DataFrame
car_records_missing<jupyter_output><empty_output><jupyter_text>Notice the missing values are represented as `NaN` in pandas DataFrames.Let's try fill them.<jupyter_code># Fill the Odometer (KM) column missing values with the mean of the column inplace
car_records_missing["Odometer"].fillna(car_records_missing["Odometer"].mean(),inplace=True)
# View the car sales missing DataFrame and verify the changes
car_records_missing
# Remove the rest of the missing data inplace
car_records_missing.dropna(inplace=True)
# Verify the missing values are removed by viewing the DataFrame
car_records_missing<jupyter_output><empty_output><jupyter_text>We'll now start to add columns to our DataFrame.<jupyter_code># Create a "Seats" column where every row has a value of 5
car_records["Seats"] = 5
# Create a column called "Engine Size" with random values between 1.3 and 4.5
# Remember: If you're doing it from a Python list, the list has to be the same length
# as the DataFrame
import random
def randomGen(length,fromm,too):
    randList = []
    for i in range(0,length):
        n = round(random.uniform(fromm,too),1)
        randList.append(n)
    return randList
car_records["Engine Size"] = randomGen(len(car_records),1.3,4.5)
# Create a column which represents the price of a car per kilometer
# Then view the DataFrame
car_records["Price per KM"] = car_records["Price"] / car_records["Odometer (KM)"]
car_records
# Remove the last column you added using .drop()
car_records.drop("Price per KM",axis=1,inplace=True)
# Shuffle the DataFrame using sample() with the frac parameter set to 1
# Save the the shuffled DataFrame to a new variable
car_records_shuffled = car_records.sample(frac=1)
car_records_shuffled<jupyter_output><empty_output><jupyter_text>Notice how the index numbers get moved around. The [`sample()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sample.html) function is a great way to get random samples from your DataFrame. It's also another great way to shuffle the rows by setting `frac=1`.<jupyter_code># Reset the indexes of the shuffled DataFrame
car_records_shuffled.reset_index()<jupyter_output><empty_output><jupyter_text>Notice the index numbers have been changed to have order (start from 0).<jupyter_code># Change the Odometer values from kilometers to miles using a Lambda function
# Then view the DataFrame
car_records["Odometer (KM)"] = car_records["Odometer (KM)"].apply(lambda x : x/1.6)
# Change the title of the Odometer (KM) to represent miles instead of kilometers
car_records.rename(columns={"Odometer (KM)":"Odometer (miles)"},inplace=True)
car_records<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09456125925055797, 'fraction_numerical': 0.010219664043228004, 'mean_word_length': 5.232796486090776, 'pattern_counts': {'":': 3, '<': 45, '<?xml version=': 0, '>': 46, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '515874', 'license': 'MIT', 'n_tokens_mistral': 2632, 'n_tokens_neox': 2517, 'n_words': 962, 'path': 'Data Analysis/Pandas/pandas-exercises.ipynb', 'repo_name': 'bhupendpatil/Practice', 'size': '8513'}