Petite Pandas Data Analysis using Pandas and NumPy
A series of lessons based on the utilization of Pandas and NumPy to read and create interesting things with files and data.
- Predictive Analysis
- 1. Intro to NumPy and the features it consists
- 2. Using NumPy to create arrays
- 3. Basic array operations
- 4. Data analysis using numpy
- Pandas
- Basics of Pandas.
- Pandas Classes
- PeriodIndex
- Dataframe Grouped By
- CSV FILES!
- Our Frontend Data Analysis Project
- Popcorn Hacks
- Main Hack
- Grading
Predictive Analysis
Predictive analysis is the use of statistical, data mining, and machine learning techniques to analyze current and historical data in order to make predictions about future events or behaviors. It involves identifying patterns and trends in data, and then using that information to forecast what is likely to happen in the future.
Predictive analysis is used in a wide range of applications, from forecasting sales and demand, to predicting customer behavior, to detecting fraudulent transactions. It involves collecting and analyzing data from a variety of sources, including historical data, customer data, financial data, and social media data, among others.
The process of predictive analysis typically involves the following steps:
- Defining the problem and identifying the relevant data sources
- Collecting and cleaning the data
- Exploring and analyzing the data to identify patterns and trends
- Selecting an appropriate model or algorithm to use for predictions
- Training and validating the model using historical data
- Using the model to make predictions on new data
- Monitoring and evaluating the performance of the model over time
Predictive analysis can help organizations make more informed decisions, improve efficiency, and gain a competitive advantage by leveraging insights from data.
It is most commonly used in Retail, where workers try to predict which products would be most popular and try to advertise those products as much as possible, and also Healthcare, where algorithms analyze patterns and reveal prerequisites for diseases and suggest preventive treatment, predict the results of various treatments and choose the best option for each patient individually, and predict disease outbreaks and epidemics.
1. Intro to NumPy and the features it consists
Numpy, by definition, is the fundamental package for scientific computing in Python which can be used to perform mathematical operations, providing multidimensional array objects, and makes data analysis much easier. Numpy is very important and useful when it comes to data analysis, as it can easily use its features to complete and perform any mathematical operation, as well as analyze data files.
If you don't already have numpy installed, you can do so using conda install numpy
or pip install numpy
Once that is complete, to import numpy in your code, all you must do is:
import numpy as np
2. Using NumPy to create arrays
An array is the central data structure of the NumPy library. They are used as containers which are able to store more than one item at the same time. Using the function np.array is used to create an array, in which you can create multidimensional arrays.
Shown below is how to create a 1D array:
a = np.array([1, 2, 3])
print(a)
# this creates a 1D array
How could you create a 3D array based on knowing how to make a 1D array?
import numpy as np
a = np.array([1, 2, 3])
a_3d = a.reshape((1, 1, 3))
print(a_3d)
Arrays can be printed in different ways, especially a more readable format. As we have seen, arrays are printed in rows and columns, but we can change that by using the reshape
function
c = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(c.reshape(1, 9)) # organizes it all in a single line of output
In the code segment below, we can also specially select certain rows and columns from the array to further analyze selective data.
print(c[1:, :2])
# the 1: means "start at row 1 and select all the remaining rows"
# the :2 means "select the first two columns"
3. Basic array operations
One of the most basic operations that can be performed on arrays is arithmetic operations. With numpy, it is very easy to perform arithmetic operations on arrays. You can add, subtract, multiply and divide arrays, just like you would with regular numbers. When performing these operations, numpy applies the operation element-wise, meaning that it performs the operation on each element in the array separately. This makes it easy to perform operations on large amounts of data quickly and efficiently.
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # adds each value based on the column the integer is in
print(a - b) # subtracts each value based on the column the integer is in
print(a * b) # multiplies each value based on the column the integer is in
print(a / b) # divides each value based on the column the integer is in
d = np.exp(b)
e = np.sqrt(b)
print(d)
print(e)
From the knowledge of how to use more advanced mathematical expressions than the basic 4 mathematical operations such as exponent and square root, now can you code how to calculate the 3 main trig expressions (sin, cos, tan), natural log, and log10 of a 1D array.
# calculate cos
# calculate tan
# calculate natural log
# calculate log10
import numpy as np
a = np.array([1, 2, 3])
# Sine of a 1D array
sin_a = np.sin(a)
print("Sine of a 1D array:", sin_a)
# Cosine of a 1D array
cos_a = np.cos(a)
print("Cosine of a 1D array:", cos_a)
# Tangent of a 1D array
tan_a = np.tan(a)
print("Tangent of a 1D array:", tan_a)
# Natural logarithm of a 1D array
ln_a = np.log(a)
print("Natural logarithm of a 1D array:", ln_a)
# Base-10 logarithm of a 1D array
log10_a = np.log10(a)
print("Base-10 logarithm of a 1D array:", log10_a)
4. Data analysis using numpy
Numpy provides a convenient and powerful way to perform data analysis tasks on large datasets. One of the most common tasks in data analysis is finding the mean, median, and standard deviation of a dataset. Numpy provides functions to perform these operations quickly and easily. The mean function calculates the average value of the data, while the median function calculates the middle value in the data. The standard deviation function calculates how spread out the data is from the mean. Additionally, numpy provides functions to find the minimum and maximum values in the data. These functions are very useful for gaining insight into the properties of large datasets and can be used for a wide range of data analysis tasks.
data = np.array([2, 5, 12, 13, 19])
print(np.mean(data)) # finds the mean of the dataset
print(np.median(data)) # finds the median of the dataset
print(np.std(data)) # finds the standard deviation of the dataset
print(np.min(data)) # finds the min of the dataset
print(np.max(data)) # finds the max of the dataset
Now from learning this, can you find a different way from how we can solve the sum or products of a dataset other than how we learned before?
import numpy as np
data = np.array([2, 5, 12, 13, 19])
# Using the np.sum() function
sum_data = np.sum(data)
print("Sum of the dataset:", sum_data)
# Using the built-in sum() function
sum_data_2 = sum(data)
print("Sum of the dataset (using built-in sum() function):", sum_data_2)
# Using a for loop
sum_data_3 = 0
for i in data:
sum_data_3 += i
print("Sum of the dataset (using a for loop):", sum_data_3)
Numpy also has the ability to handle CSV files, which are commonly used to store and exchange large datasets. By importing CSV files into numpy arrays, we can easily perform complex operations and analysis on the data, making numpy an essential tool for data scientists and researchers.
genfromtxt
and loadtxt
are two functions in the numpy library that can be used to read data from text files, including CSV files.
genfromtxt
is a more advanced function that can be used to read text files that have more complex structures, including CSV files. genfromtxt
can handle files that have missing or invalid data, or files that have columns of different data types. It can also be used to skip header lines or to read only specific columns from the file.
import numpy as np
padres = np.genfromtxt('files/padres.csv', delimiter=',', dtype=str, encoding='utf-8')
# delimiter indicates that the data is separated into columns which is distinguished by commas
# genfromtxt is used to read the csv file itself
# dtype is used to have numpy automatically detect the data type in the csv file
print(padres)
loadtxt
is a simpler function that can be used to read simple text files that have a regular structure, such as files that have only one type of data (such as all integers or all floats). loadtxt
can be faster than genfromtxt
because it assumes that the data in the file is well-structured and can be easily parsed.
import numpy as np
padres = np.loadtxt('files/padres.csv', delimiter=',', dtype=str, encoding='utf-8')
print(padres)
for i in padres:
print(",".join(i))
Pandas
What is Pandas
Pandas is a Python library used for working with data sets. A python library is something It has functions for analyzing, cleaning, exploring, and manipulating data.
Why Use Pandas?
Pandas allows us to analyze big data and make conclusions based on statistical theories. Pandas can clean messy data sets, and make them readable and relevant. Also it is a part of data analysis, and data manipulation.
What Can Pandas Do?
Pandas gives you answers about the data. Like:
- Is there a correlation between two or more columns?
- What is average value
- Max value
- Min value
- How to load data
- Delete data
- Sort Data.
Pandas are also able to delete rows that are not relevant, or contains wrong values, like empty or NULL values. This is called cleaning the data.
import pandas as pd
# What this does is it calls the python pandas library and this code segment is needed whenever incorporating pandas.
import pandas as pd
data1 = {
'teams': ["BARCA", "REAL", "ATLETICO"],
'standings': [1, 2, 3]
}
myvar = pd.DataFrame(data1)
print(myvar)
import pandas as pd
score = [5/5, 5/5, 1/5]
myvar = pd.Series(score, index = ["math", "science", "pe"])
print(myvar)
import pandas as pd
time = pd.period_range('2022-01', '2022-12', freq='M')
print(time)
Now implement a way to show a period index from June 2022 to July 2023 in days.
import pandas as pd
time = pd.period_range('2022-01', '2022-12', freq='M')
# Create a PeriodIndex with the time range
period_index = pd.PeriodIndex(time)
# Calculate the number of days between June 2022 to 2023
june_2022 = period_index[5].to_timestamp()
jan_2023 = period_index[-1].to_timestamp()
days_between = (jan_2023 - june_2022).days
# Print the result
print("Number of days between June 2022 and January 2023:", days_between)
Dataframe Grouped By
- This allows for you to organize your data and calculate the different functions such as
- count(): returns the number of non-null values in each group.
- sum(): returns the sum of values in each group.
- mean(): returns the mean of values in each group.
- min(): returns the minimum value in each group.
- max(): returns the maximum value in each group.
- median(): returns the median of values in each group.
- var(): returns the variance of values in each group.
- agg(): applies one or more functions to each group and returns a new DataFrame with the results.
import pandas as pd
data = {
'Category': ['E', 'F', 'E', 'F', 'E', 'F', 'E', 'F'],
'Value': [100, 250, 156, 255, 240, 303, 253, 3014]
}
df = pd.DataFrame(data)
grouped = df.groupby('Category') #GUESS WHAT THIS WOULD BE IF WE WERE LOOKING FOR COMBINED TOTALS!()
print(grouped)
import pandas as pd
colors = pd.Categorical(['yellow', 'orange', 'blue', 'yellow', 'orange'], categories=['yellow', 'orange', 'blue'])
print(colors)
import pandas as pd
timing = pd.Timestamp('2023-02-05 02:00:00')
print(timing)
- Name, Position, Average, HR, RBI, OPS, JerseyNumber
- Manny Machado, 3B, .298, 32, 102, .897, 13
- Tatis Jr, RF, .281, 42, 97, .975, 23
- Juan Soto, LF, .242, 27, 62, .853, 22
- Xanger Bogaerts, SS, .307, 15, 73, .833, 2
- Nelson Cruz, DH, .234, 10, 64, .651, 32
- Matt Carpenter, DH, .305, 15, 37, 1.138, 14
- Cronezone, 1B, .239, 17, 88, .722, 9
- Ha-Seong Kim, 2B, .251, 11, 59, .708, 7
- Trent Grisham, CF, .184, 17, 53, .626, 1
- Luis Campusano, C, .250, 1, 5, .593, 12
- Austin Nola, C, .251, 4, 40, .649, 26
- Jose Azocar, OF, .257, 0, 10, .630, 28
QUESTION: WHAT DO YOU GUYS THINK THE INDEX FOR THIS WOULD BE? It seems like the index for this would be player names
Can you explain what is going on in this code segment below. (hint: define what ascending= false means, and df. head means)
import pandas as pd
#read csv and sort 'Duration' largest to smallest
df = pd.read_csv('files/padres.csv').sort_values(by=['Name'], ascending=False)
print("--Duration Top 10---------")
print(df.head(10))
print("--Duration Bottom 10------")
print(df.tail(10))
print(', '.join(df.tail(10)))
import pandas as pd
df = pd.read_csv("./files/housing.csv")
mode_total_rooms = df['total_rooms'].mode()
print(f"The mode of the 'total_rooms' column is: {mode_total_rooms}")
import pandas as pd
df = pd.read_csv("./files/housing.csv")
grouped_df = df.groupby('total_rooms')
agg_df = grouped_df.agg({'total_rooms': 'sum', 'population': 'mean', 'longitude': 'count'})
# WHAT DO YOU GUYS THINK df.agg means in context of pandas and what does it stand for.
print(agg_df)
Popcorn Hacks
- Complete fill in the blanks for Predictive Analysis Numpy (done)
- Takes notes on Panda where it asks you to (done)
- Complete code segment tasks in Panda and Numpy (done)
Main Hack
- Make a data file - content is up to you, just make sure there are integer values - and print
- Run Panda and Numpy commands
- Panda:
- Find Min and Max values
- Sort in order - can be order of least to greatest or vice versa
- Create a smaller dataframe and merge it with your data file
- Numpy:
- Random number generation
- create a multi-dimensional array (multiple elements)
- create an array with linearly spaced intervals between values
- Panda:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [25, 32, 18, 45, 28],
'Height': [160, 175, 165, 180, 170],
'Weight': [50, 70, 60, 85, 65]
}
df = pd.DataFrame(data)
df.to_csv('data.csv', index=False)
import pandas as pd
import numpy as np
df = pd.read_csv("./files/hacks.csv")
# Print the data frame
print(df)
# Find the min and max values
print("Minimum age:", df['Age'].min())
print("Maximum weight:", df['Weight'].max())
# Sort in ascending order by age
df_sorted = df.sort_values('Age')
print("Data frame sorted by age:")
print(df_sorted)
# Create a smaller dataframe and merge it with the original dataframe
df_small = df[['Name', 'Height']]
df_merged = pd.merge(df, df_small, on='Name')
print("Merged data frame:")
print(df_merged)
# Generate random numbers
random_array = np.random.randint(low=0, high=100, size=(3, 4))
print("Random array:")
print(random_array)
# Create a multi-dimensional array
multi_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Multi-dimensional array:")
print(multi_array)
# Create an array with linearly spaced intervals
linear_array = np.linspace(start=0, stop=10, num=5)
print("Linearly spaced array:")
print(linear_array)