<jupyter_start><jupyter_text>Pandas Introduction  Two main data types Series and Data frame<jupyter_code>import pandas as pd
# Series: 1-dimensional
series = pd.Series(['BMW', 'Toyota', 'Honda'])
series
colours = pd.Series(['Green', 'Red', 'Yellow'])
colours
# Data frame 2-dimensional
cars = pd.DataFrame({
    '<NAME>': series, 
    'Colours': colours
})
cars
cars_sales = pd.read_csv('../data/car-sales.csv')
cars_sales
# axis=0 refers to rows
# axis=1 refers to columns<jupyter_output><empty_output><jupyter_text>Describing Data<jupyter_code>cars_sales.dtypes
cars_sales.columns
cars_sales.index
cars_sales.describe() # work only with numeric values
cars_sales.info()
notes = pd.Series([10, 9, 9])
notes.mean()
notes.sum()
cars_sales['Doors'].sum() # selecting a single column
len(cars_sales) # number of rows
len(cars_sales) # Number of rows in the data frame<jupyter_output><empty_output><jupyter_text>View and selecting data<jupyter_code>cars_sales.head()
cars_sales.head(7)
cars_sales.tail()
cars_sales.tail(3)<jupyter_output><empty_output><jupyter_text>iloc and loc<jupyter_code>animals = pd.Series(['snake', 'dog', 'bird', 'chicken', 'elephant'],
                    index=[3, 5, 1, 0, 8])
animals
animals.loc[0] # loc refers to indexes
animals.iloc[0] # iloc refers to position
animals.iloc[:3]
animals.iloc[-3:]
animals[0] # It uses loc
cars_sales.head(3)
cars_sales['Make'] # selecting a column
cars_sales.Make # the same as the line above, don't use this one because does not support spaces
# Making some querys
cars_sales[cars_sales['Make'] == 'Toyota']
cars_sales[cars_sales['Odometer (KM)'] > 100_000]
cars_sales
pd.crosstab(cars_sales['Make'], cars_sales['Doors'])
cars_sales.mean()
cars_sales.groupby(['Make']).mean()
# %matplotlib inline      to plot in the Notebook
# import matplotlib.pyplot as plt

cars_sales['Odometer (KM)'].plot() # Plot the different tables of the Odometer
cars_sales['Odometer (KM)'].hist()
# Object to a numeric type
cars_sales['Price'] = cars_sales['Price'].str.replace('[\$\,\.]', '').astype(int)
cars_sales['Price'] = cars_sales['Price'] / 100
cars_sales
cars_sales['Price'].dtype<jupyter_output><empty_output><jupyter_text>Manipulating Data<jupyter_code>cars_sales['Make'] = cars_sales['Make'].str.lower()
cars_sales.head(2)
cars_sales_missing = pd.read_csv('../data/car-sales-missing-data.csv')
cars_sales_missing # NaN stands for missing data
# Filling missing data
cars_sales_missing['Odometer'].fillna(0)
cars_sales_missing
# as the previous example we have to reasign the value of the column we changed as follows:
# cars_sales_missing['Odometer'] = cars_sales_missing['Odometer'].fillna(0)
# we can use the inplace parameter to avoid doing this every time (by defaul inplace is False)
cars_sales_missing['Odometer'].fillna(0, inplace=True)
cars_sales_missing<jupyter_output><empty_output><jupyter_text>Dropping the NaN values<jupyter_code>cars_sales_missing.dropna(inplace=True) # with this we drop all the rows that have a NaN value
cars_sales_missing<jupyter_output><empty_output><jupyter_text>Creating Columns<jupyter_code># Column from series
seats_column = pd.Series([5, 5, 5, 5, 5])

# Creating new column called seats
cars_sales['Seats'] = seats_column
cars_sales
cars_sales.fillna(5, inplace=True)
cars_sales
cars_sales['Fuel per 100KM'] = pd.Series([7.5, 9.2, 5.0, 9.6, 8.7, 4.6, 7.6, 8.7, 3.0, 4.5])
cars_sales
cars_sales['Total fuel used'] = cars_sales['Odometer (KM)'] / 10 * cars_sales['Fuel per 100KM']
cars_sales
# Creating a column from a value
cars_sales['Number of wheels'] = 4
cars_sales<jupyter_output><empty_output><jupyter_text>Droping a column<jupyter_code>cars_sales.drop('Number of wheels', axis=1, inplace=True) # remember that axis 1 is for columns and 0 for rows
cars_sales<jupyter_output><empty_output><jupyter_text>Taking samples<jupyter_code># We can take samples of the dataset, these samples are shuffled
cars_sales.sample(frac=1) # fraction take the porcentage of the sample
cars_sales.sample(frac=0.5)
cars_sales_shuffled = cars_sales.sample(frac=1)
cars_sales_shuffled
# restoring the indexes
cars_sales_shuffled.reset_index() # it add a new column called index
cars_sales_shuffled.reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>Using *apply* to a column<jupyter_code># transforming the kilometers to miles
cars_sales['Odometer (KM)'] = cars_sales['Odometer (KM)'].apply(lambda x: x / 1.6)
cars_sales<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1287998198603918, 'fraction_numerical': 0.017563611799144337, 'mean_word_length': 6.903914590747331, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '382909', 'license': 'MIT', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1547, 'n_words': 381, 'path': 'notebooks/Pandas.ipynb', 'repo_name': 'CristoferNava/Machine-Learning', 'size': '4446'}