import pandas as pd
import numpy as np
a
:= [0.1, 1.2, 2.3, 3.4, 4.5] a
Write a Python code that uses the list, a
, to create the
following Numpy Array:
Answer
= np.array(a) arr_a
Write a Python code that uses the list, a
, to create the
following pandas Series:
########################
# Index 0
# 0 0.1
# 1 1.2
# 2 2.3
# 3 3.4
# 4 4.5
# dtype: float64
########################
Answer
= pd.Series(a)
s s
Write a Python code that uses the list, a
, to create the
following pandas Series:
########################
# Index 0
# a 0.1
# b 1.2
# c 2.3
# d 3.4
# e 4.5
# dtype: float64
########################
Answer
= pd.Series(a,
s = ['a','b','c','d','e'])
index s
Write a Python code that uses (1) the Series created in Q1c and (2) Boolean indexing to get the following Series:
########################
# Index 0
# c 2.3
# d 3.4
# e 4.5
# dtype: float64
########################
Answer
> 2 ] s[ s
The next line creates a list of tuples that are percentiles and Household Incomes at the specified percentiles
= [ (10, 14629), (20, 25600), (30, 37002),
hh_income 40, 50000), (50, 63179), (60, 79542),
(70, 100162), (80, 130000), (90, 184292) ] (
Write a Python code that uses the list, hh_income, to assign the
object, hh_income_array
, to the following Numpy array:
############################
# array([[ 10, 14629],
# [ 20, 25600],
# [ 30, 37002],
# [ 40, 50000],
# [ 50, 63179],
# [ 60, 79542],
# [ 70, 100162],
# [ 80, 130000],
# [ 90, 184292]])
############################
Answer
= np.array(hh_income) hh_income_array
Write a Python code that uses the print()
function to
report the dimensions of the ndarray and the number of elements in
hh_income_array
as follows:
Answer
print("Dimensions of the NumPy array, hh_income_array, is: ", hh_income_array.shape)
print("Number of elements in the NumPy array, hh_income_array, is: ", hh_income_array.size)
c
:= np.array([ [1.0, 2], [3, 4] ]) c
Write a Python code that uses the NumPy array, c
, to
create the following DataFrame:
############################
# index 0 1
# 0 1.0 2.0
# 1 3.0 4.0
############################
Answer
= pd.DataFrame(c)
df df
Write a Python code that uses the NumPy array, c
, to
create the following DataFrame:
############################
# index dogs cats
# 0 1.0 2.0
# 1 3.0 4.0
############################
Answer
= pd.DataFrame(c, columns=['dogs','cats'])
df df
Write a Python code that uses the NumPy array, c
, to
create the following DataFrame:
############################
# index dogs cats
# byeong-hak 1.0 2.0
# your_first_name 3.0 4.0
############################
Answer
= pd.DataFrame(c,
df =['dogs','cats'],
columns= ['byeong-hak', 'your_first_name'])
index df
Download the file, US_state_GDP.zip, from the Files section in our
Canvas. Extract the zip file, US_state_GDP.zip, to use the CSV file,
US_state_GDP.csv
.
Assign path_csv to the string of the absolute pathname of the file,
US_state_GDP.csv
.
####################################################################################################################################
# For example
# path_csv = '/Users/byeong-hakchoe/Google Drive/suny-geneseo/teaching-materials/lecture-data/US_state_GDP.csv'
# path_csv = 'C:/byeong-hakchoe/Google Drive/suny-geneseo/teaching-materials/lecture-data/US_state_GDP.csv'
####################################################################################################################################
Read the data file, US_state_GDP.csv, as the object name,
state_gdp
, using (1) path_csv
and (2)
pd.read_csv()
function.
Answer
# This is an example of the absolute path of the CSV file
= '/Users/byeong-hakchoe/Google Drive/suny-geneseo/teaching-materials/lecture-data/US_state_GDP.csv'
path_csv = pd.read_csv(path_csv) state_gdp
Write a Python code that uses the DataFrame, state_gdp
,
to create the DataFrame, whose first five rows are as follows:
############################################
# index state_code state
# 0 AK Alaska
# 1 AL Alabama
# 2 AR Arkansas
# 3 AZ Arizona
# 4 CA California
############################################
Answer
'state_code', 'state' ] ] state_gdp[ [
Write a Python code that uses (1) the DataFrame,
state_gdp
, and (2) state_gdp.columns
to create
the DataFrame, whose first five rows are as follows:
############################################
# state gdp_2009
# 0 Alaska 44215
# 1 Alabama 149843
# 2 Arkansas 89776
# 3 Arizona 221405
# 4 California 1667152
############################################
Answer
= state_gdp.columns
cols 1:3] ] state_gdp[ cols[
Write a Python code to get the first three rows of the DataFrame,
state_gdp
:
Answer
1:3 ] state_gdp[
Write a Python code to get all the rows of the DataFrame,
state_gdp
, for which the value of
gdp_growth_2010
is less than 0
Answer
= state_gdp['gdp_growth_2010'] < 0
state_long_recession state_gdp[ state_long_recession ]
Write a Python code that uses state_gdp.loc[]
to get the
following DataFrame:
############################################
# state gdp_growth_2010
# 0 Alaska -1.7
# 3 Arizona -0.2
# 33 Nevada -0.4
# 50 Wyoming -1.3
############################################
Answer
'state', 'gdp_growth_2010'] ] state_gdp.loc[ state_long_recession,[
Write a Python code that uses state_gdp.iloc[]
to get
the following DataFrame:
############################################
# state_code state
# 10 GA Georgia
# 11 HI Hawaii
# 12 IA Iowa
# 13 ID Idaho
# 14 IL Illinois
############################################
Answer
10:15, :2 ] state_gdp.iloc[