CHP 8 Pandas
CHP 8 Pandas
CHP 8 Pandas
• import pandas
mydataset = {
'cars':
["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}
myvar =
pandas.DataFrame(mydataset)
print(myvar)
Pandas as pd
• import pandas as pd
mydataset = {
'cars':
["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}
myvar =
pd.DataFrame(mydataset)
print(myvar)
Pandas Series
• A Pandas Series is like a column in a table.
• It is a one-dimensional array holding data of any type.
Example: Create a simple Pandas Series from a list:
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)
Labels
• If nothing else is specified, the values are labeled with
their index number. First value has index 0, second
value has index 1 etc.
• This label can be used to access a specified value.
• Example
• Return the first value of the Series:
print(myvar[0])
Create Labels
• With the index argument, you can name your own labels.
Example: Create your own labels:
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a, index = ["x", "y", "z"])
print(myvar)
• When you have created labels, you can access an item by referring to
the label.
Example
Return the value of "y":
print(myvar["y"])
Key/Value Objects as Series
• You can also use a key/value object, like a dictionary, when creating a
Series.
import pandas as pd
calories = {"day1": 420, "day2": 380, "day3": 390}
myvar = pd.Series(calories)
print(myvar)
Key/Value Objects as Series
• To select only some of the items in the dictionary, use the index argument
and specify only the items you want to include in the Series.
Example: Create a Series using only data from "day1" and "day2":
import pandas as pd
print(myvar)
Data Frames
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
print(df)
Locate Row
• As you can see from the result above, the DataFrame is like a table with
rows and columns.
• Pandas use the loc attribute to return one or more specified row(s)
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
df = pd.DataFrame(data, index = ["day1", "day2", "day3"])
print(df)
Locate Named Indexes
• Use the named index in the loc attribute to return the specified
row(s).
• Example: Return "day2"
#refer to the named index:
print(df.loc["day2"])
Load Files Into a DataFrame
• If your data sets are stored in a file, Pandas can load
them into a DataFrame.
• Example: Load a comma separated file (CSV file) into a
DataFrame:
import pandas as pd
df = pd.read_csv('data.csv')
print(df)
Pandas Read CSV
• A simple way to store big data sets is to use CSV files (comma
separated files).
• CSV files contains plain text and is a well know format that can be
read by everyone including Pandas.
• In our examples we will be using a CSV file called 'data.csv’.
• Load the CSV into a DataFrame:
import pandas as pd
df = pd.read_csv('data.csv')
print(df.to_string())
Pandas Read CSV
• If you have a large DataFrame with many rows, Pandas will only
return the first 5 rows, and the last 5 rows:
• Example: Print the DataFrame without the to_string() method:
import pandas as pd
df = pd.read_csv('data.csv')
print(df)
max_rows
• The number of rows returned is defined in Pandas option settings.
• You can check your system's maximum rows with the
pd.options.display.max_rows statement.
• Example
Check the number of maximum returned rows:
import pandas as pd
print(pd.options.display.max_rows)
• In my system the number is 60, which means that if the DataFrame contains
more than 60 rows, the print(df) statement will return only the headers and the
first and last 5 rows.
• You can change the maximum rows number with the same statement.
• Example
• Increase the maximum number of rows to display the entire DataFrame:
import pandas as pd
pd.options.display.max_rows = 9999
df = pd.read_csv('data.csv')
print(df)
Pandas Read JSON
• Big data sets are often stored, or extracted as JSON.
• JSON is plain text, but has the format of an object, and is well known in
the world of programming, including Pandas.
• In our examples we will be using a JSON file called 'data.json’.
• Example
• Load the JSON file into a DataFrame:
import pandas as pd
df = pd.read_json('data.json')
print(df.to_string())
Dictionary as JSON
• JSON objects have the same format as Python dictionaries.
• If your JSON code is not in a file, but in a Python Dictionary, you can
load it into a DataFrame directly:
• Example
• Load a Python Dictionary into a DataFrame:
• import pandas as pd
data = {
"Duration":{
"0":60,
"1":60,
"2":60,
"3":45,
"4":45,
"5":60
},
"Pulse":{
"0":110,
"1":117,
"2":103,
"3":109,
"4":117,
"5":102
},
"Maxpulse":{
"0":130,
"1":145,
"2":135,
"3":175,
"4":148,
"5":127
},
"Calories":{
"0":409,
"1":479,
"2":340,
"3":282,
"4":406,
"5":300
}
}
df = pd.DataFrame(data)
print(df)
Pandas - Analyzing DataFrames: Viewing
the Data
• One of the most used method for getting a quick overview of the DataFrame,
is the head() method.
• The head() method returns the headers and a specified number of rows,
starting from the top.
• Example
Get a quick overview by printing the first 10 rows of the DataFrame:
import pandas as pd
df = pd.read_csv('data.csv')
print(df.head(10))
• There is also a tail() method for viewing the last rows of the
DataFrame.
• The tail() method returns the headers and a specified number of
rows, starting from the bottom.
• Example
Print the last 5 rows of the DataFrame:
print(df.tail())
Info About the Data
• The DataFrames object has a method called info(), that gives you
more information about the data set.
• Example
Print information about the data:
print(df.info())
Pandas - Cleaning Data
• Data cleaning means fixing bad data in your data set.
• Bad data could be:
• Empty cells
• Data in wrong format
• Wrong data
• Duplicates
Pandas - Cleaning Empty Cells
• Empty cells can potentially give you a wrong result when you analyze data.
• One way to deal with empty cells is to remove rows that contain empty cells.
• This is usually OK, since data sets can be very big, and removing a few rows will not have
a big impact on the result.
• Example: Return a new Data Frame with no empty cells:
import pandas as pd
df = pd.read_csv('data2.csv')
new_df = df.dropna()
print(new_df.to_string())
• If you want to change the original DataFrame, use the inplace = True
argument:
• Example: Remove all rows with NULL values:
import pandas as pd
df = pd.read_csv('data2.csv')
df.dropna(inplace = True)
print(df.to_string())
Replace Empty Values
• Another way of dealing with empty cells is to insert a new value instead.
• This way you do not have to delete entire rows just because of some empty cells.
• The fillna() method allows us to replace empty cells with a value:
• Example
#Replace NULL values with the number 130:
import pandas as pd
df = pd.read_csv('data2.csv')
import pandas as pd
df = pd.read_csv('data.csv')
import pandas as pd
df = pd.read_csv('data.csv')
x = df["Calories"].mean()
import pandas as pd
df = pd.read_csv('data.csv')
x = df["Calories"].median()
import pandas as pd
df = pd.read_csv('data.csv')
x = df["Calories"].mode()[0]
df = pd.read_csv('data3.csv')
df['Date'] = pd.to_datetime(df['Date'])
print(df.to_string())
Removing Rows
• The result from the converting in the example above gave us a NaT
value, which can be handled as a NULL value, and we can remove the
row by using the dropna() method.
• Sometimes you can spot wrong data by looking at the data set, because you have an
expectation of what it should be.
• If you take a look at our data set, you can see that in row 7, the duration is 450, but for all the
other rows the duration is between 30 and 60.
• It doesn't have to be wrong, but taking in consideration that this is the data set of someone's
workout sessions, we conclude with the fact that this person did not work out in 450
minutes.
• How can we fix wrong values, like the one for "Duration" in row 7?
Replacing Values
• One way to fix wrong values is to replace them with something else.
• In our example, it is most likely a typo, and the value should be "45"
instead of "450", and we could just insert "45" in row 7:
• Example: Set "Duration" = 45 in row 7:
df.loc[7, 'Duration'] = 45
• For small data sets you might be able to replace the wrong data one
by one, but not for big data sets.
• To replace wrong data for larger data sets you can create some rules,
e.g. set some boundaries for legal values, and replace any values that
are outside of the boundaries.
Replacing Values
Example: Loop through all values in the "Duration" column.
• If the value is higher than 100, set it to 60:
import pandas as pd
df = pd.read_csv('data2.csv')
for x in df.index:
if df.loc[x, "Duration"] > 100:
df.loc[x, "Duration"] = 60
print(df.to_string())
Removing Rows
• Another way of handling wrong data is to remove the rows that
contains wrong data.
• This way you do not have to find out what to replace them with, and
there is a good chance you do not need them to do your analyses.
• Example: Delete rows where "Duration" is higher than 120:
for x in df.index:
if df.loc[x, "Duration"] > 120:
df.drop(x, inplace = True)
Pandas - Removing Duplicates
• Duplicate rows are rows that have been registered more than one time.
• By taking a look at our test data set, we can assume that row 11 and 12
are duplicates.
df.drop_duplicates(inplace = True)
Pandas - Data Correlations (Finding Relationship)
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
df.plot()
plt.show()
Scatter Plot
Example:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
df.plot(kind = 'scatter', x = 'Duration', y =
'Calories')
plt.show()
• Example: A scatterplot where
there are no relationship between
the columns:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
df.plot(kind = 'scatter', x =
'Duration', y = 'Maxpulse')
plt.show()
Histogram