TillPandas
This commit is contained in:
@ -0,0 +1,45 @@
|
||||
# Numpy arrays are great alternatives to Python Lists. Some of the key advantages of Numpy arrays are that they are fast, easy to work with, and give users the opportunity to perform calculations across entire arrays.
|
||||
# In the following example, you will first create two Python lists. Then, you will import the numpy package and create numpy arrays out of the newly created lists.
|
||||
|
||||
# Create 2 new lists height and weight
|
||||
height = [1.87, 1.87, 1.82, 1.91, 1.90, 1.85]
|
||||
weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]
|
||||
|
||||
# Import the numpy package as np
|
||||
import numpy as np
|
||||
|
||||
# Create 2 numpy arrays from height and weight
|
||||
np_height = np.array(height)
|
||||
np_weight = np.array(weight)
|
||||
|
||||
print(type(np_height))
|
||||
|
||||
# Now we can perform element-wise calculations on height and weight. For example, you could take all 6 of the height and weight observations above, and calculate the BMI for each observation with a single equation. These operations are very fast and computationally efficient. They are particularly helpful when you have 1000s of observations in your data.
|
||||
|
||||
# Calculate bmi
|
||||
bmi = np_weight / np_height ** 2
|
||||
|
||||
# Print the result
|
||||
print(bmi)
|
||||
|
||||
|
||||
#Another great feature of Numpy arrays is the ability to subset. For instance, if you wanted to know which observations in our BMI array are above 26, we could quickly subset it to find out.
|
||||
# For a boolean response
|
||||
print(bmi > 26)
|
||||
# Print only those observations above 26
|
||||
print(bmi[bmi > 26])
|
||||
|
||||
# Exercise
|
||||
# First, convert the list of weights from a list to a Numpy array. Then, convert all of the weights from kilograms to pounds. Use the scalar conversion of 2.2 lbs per kilogram to make your conversion. Lastly, print the resulting array of weights in pounds.
|
||||
|
||||
weight_kg = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]
|
||||
import numpy as np
|
||||
|
||||
# Create a numpy array np_weight_kg from weight_kg
|
||||
npWeight = np.array(weight_kg)
|
||||
|
||||
# Create np_weight_lbs from np_weight_kg
|
||||
npWeightLbs = npWeight * 2.2
|
||||
|
||||
# Print out np_weight_lbs
|
||||
print(npWeight)
|
28
DataScienceTutorials/2_PandasBasics.py
Normal file
28
DataScienceTutorials/2_PandasBasics.py
Normal file
@ -0,0 +1,28 @@
|
||||
# Pandas is a high-level data manipulation tool developed by Wes McKinney. It is built on the Numpy package and its key data structure is called the DataFrame. DataFrames allow you to store and manipulate tabular data in rows of observations and columns of variables.
|
||||
# There are several ways to create a DataFrame. One way way is to use a dictionary. For example:
|
||||
dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"],
|
||||
"capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"],
|
||||
"area": [8.516, 17.10, 3.286, 9.597, 1.221],
|
||||
"population": [200.4, 143.5, 1252, 1357, 52.98] }
|
||||
|
||||
import pandas as pd
|
||||
brics = pd.DataFrame(dict)
|
||||
print(brics)
|
||||
|
||||
#As you can see with the new brics DataFrame, Pandas has assigned a key for each country as the numerical values 0 through 4. If you would like to have different index values, say, the two letter country code, you can do that easily as well.
|
||||
# Set the index for brics
|
||||
brics.index = ["BR", "RU", "IN", "CH", "SA"]
|
||||
|
||||
# Print out brics with new index values
|
||||
print(brics)
|
||||
|
||||
|
||||
#Another way to create a DataFrame is by importing a csv file using Pandas. Now, the csv cars.csv is stored and can be imported using pd.read_csv:
|
||||
|
||||
# Import the cars.csv data: cars
|
||||
cars = pd.read_csv('cars.csv')
|
||||
|
||||
# Print out cars
|
||||
print(cars)
|
||||
|
||||
#https://www.learnpython.org/en/Pandas_Basics
|
54
DataScienceTutorials/cars.csv
Normal file
54
DataScienceTutorials/cars.csv
Normal file
@ -0,0 +1,54 @@
|
||||
YEAR,Make,Model,Size,(kW),Unnamed: 5,TYPE,CITY (kWh/100 km),HWY (kWh/100 km),COMB (kWh/100 km),CITY (Le/100 km),HWY (Le/100 km),COMB (Le/100 km),(g/km),RATING,(km),TIME (h)
|
||||
2012,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
|
||||
2012,NISSAN,LEAF,MID-SIZE,80,A1,B,19.3,23.0,21.1,2.2,2.6,2.4,0,n/a,117,7
|
||||
2013,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,n/a,122,4
|
||||
2013,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
|
||||
2013,NISSAN,LEAF,MID-SIZE,80,A1,B,19.3,23.0,21.1,2.2,2.6,2.4,0,n/a,117,7
|
||||
2013,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
|
||||
2013,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
|
||||
2013,TESLA,MODEL S (40 kWh battery),FULL-SIZE,270,A1,B,22.4,21.9,22.2,2.5,2.5,2.5,0,n/a,224,6
|
||||
2013,TESLA,MODEL S (60 kWh battery),FULL-SIZE,270,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,n/a,335,10
|
||||
2013,TESLA,MODEL S (85 kWh battery),FULL-SIZE,270,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
|
||||
2013,TESLA,MODEL S PERFORMANCE,FULL-SIZE,310,A1,B,23.9,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
|
||||
2014,CHEVROLET,SPARK EV,SUBCOMPACT,104,A1,B,16.0,19.6,17.8,1.8,2.2,2.0,0,n/a,131,7
|
||||
2014,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,n/a,122,4
|
||||
2014,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
|
||||
2014,NISSAN,LEAF,MID-SIZE,80,A1,B,16.5,20.8,18.4,1.9,2.3,2.1,0,n/a,135,5
|
||||
2014,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
|
||||
2014,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
|
||||
2014,TESLA,MODEL S (60 kWh battery),FULL-SIZE,225,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,n/a,335,10
|
||||
2014,TESLA,MODEL S (85 kWh battery),FULL-SIZE,270,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
|
||||
2014,TESLA,MODEL S PERFORMANCE,FULL-SIZE,310,A1,B,23.9,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
|
||||
2015,BMW,i3,SUBCOMPACT,125,A1,B,15.2,18.8,16.8,1.7,2.1,1.9,0,n/a,130,4
|
||||
2015,CHEVROLET,SPARK EV,SUBCOMPACT,104,A1,B,16.0,19.6,17.8,1.8,2.2,2.0,0,n/a,131,7
|
||||
2015,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,n/a,122,4
|
||||
2015,KIA,SOUL EV,STATION WAGON - SMALL,81,A1,B,17.5,22.7,19.9,2.0,2.6,2.2,0,n/a,149,4
|
||||
2015,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
|
||||
2015,NISSAN,LEAF,MID-SIZE,80,A1,B,16.5,20.8,18.4,1.9,2.3,2.1,0,n/a,135,5
|
||||
2015,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
|
||||
2015,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
|
||||
2015,TESLA,MODEL S (60 kWh battery),FULL-SIZE,283,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,n/a,335,10
|
||||
2015,TESLA,MODEL S (70 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,377,12
|
||||
2015,TESLA,MODEL S (85/90 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
|
||||
2015,TESLA,MODEL S 70D,FULL-SIZE,280,A1,B,20.8,20.6,20.7,2.3,2.3,2.3,0,n/a,386,12
|
||||
2015,TESLA,MODEL S 85D/90D,FULL-SIZE,280,A1,B,22.0,19.8,21.0,2.5,2.2,2.4,0,n/a,435,12
|
||||
2015,TESLA,MODEL S P85D/P90D,FULL-SIZE,515,A1,B,23.4,21.5,22.5,2.6,2.4,2.5,0,n/a,407,12
|
||||
2016,BMW,i3,SUBCOMPACT,125,A1,B,15.2,18.8,16.8,1.7,2.1,1.9,0,10,130,4
|
||||
2016,CHEVROLET,SPARK EV,SUBCOMPACT,104,A1,B,16.0,19.6,17.8,1.8,2.2,2.0,0,10,131,7
|
||||
2016,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,10,122,4
|
||||
2016,KIA,SOUL EV,STATION WAGON - SMALL,81,A1,B,17.5,22.7,19.9,2.0,2.6,2.2,0,10,149,4
|
||||
2016,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,10,100,7
|
||||
2016,NISSAN,LEAF (24 kWh battery),MID-SIZE,80,A1,B,16.5,20.8,18.4,1.9,2.3,2.1,0,10,135,5
|
||||
2016,NISSAN,LEAF (30 kWh battery),MID-SIZE,80,A1,B,17.0,20.7,18.6,1.9,2.3,2.1,0,10,172,6
|
||||
2016,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,10,109,8
|
||||
2016,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,10,109,8
|
||||
2016,TESLA,MODEL S (60 kWh battery),FULL-SIZE,283,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,10,335,10
|
||||
2016,TESLA,MODEL S (70 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,10,377,12
|
||||
2016,TESLA,MODEL S (85/90 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,10,426,12
|
||||
2016,TESLA,MODEL S 70D,FULL-SIZE,386,A1,B,20.8,20.6,20.7,2.3,2.3,2.3,0,10,386,12
|
||||
2016,TESLA,MODEL S 85D/90D,FULL-SIZE,386,A1,B,22.0,19.8,21.0,2.5,2.2,2.4,0,10,435,12
|
||||
2016,TESLA,MODEL S 90D (Refresh),FULL-SIZE,386,A1,B,20.8,19.7,20.3,2.3,2.2,2.3,0,10,473,12
|
||||
2016,TESLA,MODEL S P85D/P90D,FULL-SIZE,568,A1,B,23.4,21.5,22.5,2.6,2.4,2.5,0,10,407,12
|
||||
2016,TESLA,MODEL S P90D (Refresh),FULL-SIZE,568,A1,B,22.9,21.0,22.1,2.6,2.4,2.5,0,10,435,12
|
||||
2016,TESLA,MODEL X 90D,SUV - STANDARD,386,A1,B,23.2,22.2,22.7,2.6,2.5,2.6,0,10,414,12
|
||||
2016,TESLA,MODEL X P90D,SUV - STANDARD,568,A1,B,23.6,23.3,23.5,2.7,2.6,2.6,0,10,402,12
|
|
Reference in New Issue
Block a user