Import NumPy under the alias np
.
import numpy as np
Import pandas under the alias pd
.
import pandas as pd
Given the following NumPy array data
, create a pandas DataFrame named first_data_frame
that contains the same elements. Print the DataFrame to make sure the operation executed successfully.
data = np.round(np.random.randn(5,5),1)
#Solution goes here
first_data_frame = pd.DataFrame(data)
first_data_frame
Assign the values of row_labels
to the index of first_data_frame
. Print the DataFrame to make sure the operation executed successfully.
Hint: It will be easier to overwrite first_data_frame
by using another pd.DataFrame
method.
row_labels = ['one','two','three','four','five']
#Solution goes here
first_data_frame = pd.DataFrame(data,row_labels)
first_data_frame
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
one | -1.9 | 1.8 | 0.6 | 0.0 | 1.1 |
two | 2.3 | -2.1 | -0.3 | -1.8 | -0.6 |
three | 0.7 | 0.4 | -1.0 | -0.7 | 0.3 |
four | 0.8 | 0.5 | -0.7 | 0.0 | 0.2 |
five | 1.7 | 1.0 | -1.3 | 1.8 | 1.3 |
Assign the values of column_labels
to the columns of first_data_frame
. Note that there are two main ways to do this - you are free to chose the method of your choice. Print the DataFrame to make sure the operation executed successfully.
column_labels = ['alpha','beta','charlie','delta','echo']
#Solution goes here
first_data_frame.columns = column_labels
first_data_frame
alpha | beta | charlie | delta | echo | |
---|---|---|---|---|---|
one | -1.9 | 1.8 | 0.6 | 0.0 | 1.1 |
two | 2.3 | -2.1 | -0.3 | -1.8 | -0.6 |
three | 0.7 | 0.4 | -1.0 | -0.7 | 0.3 |
four | 0.8 | 0.5 | -0.7 | 0.0 | 0.2 |
five | 1.7 | 1.0 | -1.3 | 1.8 | 1.3 |
Create a pandas Series named my_series
that contains the values from row alpha
of first_data_frame
. Print my_series
to make sure the operation executed successfully.
#Solution goes here
my_series = first_data_frame['alpha']
my_series
one -1.9 two 2.3 three 0.7 four 0.8 five 1.7 Name: alpha, dtype: float64
Create a new DataFrame called second_data_frame
that is equal to first_data_frame
but without row one
. Print second_data_frame
to make sure the operation executed successfully.
#Solution goes here
second_data_frame = first_data_frame.drop('one')
second_data_frame
alpha | beta | charlie | delta | echo | |
---|---|---|---|---|---|
two | 2.3 | -2.1 | -0.3 | -1.8 | -0.6 |
three | 0.7 | 0.4 | -1.0 | -0.7 | 0.3 |
four | 0.8 | 0.5 | -0.7 | 0.0 | 0.2 |
five | 1.7 | 1.0 | -1.3 | 1.8 | 1.3 |
Create a new DataFrame called third_data_frame
that is equal to second_data_frame
, but without row charlie
. Print third_data_frame
to make sure the operation executed successfully.
#Solution goes here
third_data_frame = second_data_frame.drop('charlie', axis=1)
third_data_frame
alpha | beta | delta | echo | |
---|---|---|---|---|
two | 2.3 | -2.1 | -1.8 | -0.6 |
three | 0.7 | 0.4 | -0.7 | 0.3 |
four | 0.8 | 0.5 | 0.0 | 0.2 |
five | 1.7 | 1.0 | 1.8 | 1.3 |
Create a variable called row_two
that is equal to row two
from third_data_frame
. Print row_two
to make sure the operation executed successfully.
#Solution goes here
row_two = third_data_frame.loc['two']
row_two
alpha 2.3 beta -2.1 delta -1.8 echo -0.6 Name: two, dtype: float64
Print the shape of new_data.
new_data = np.round(np.random.randn(5,5),1)
#Solution goes here
new_data.shape
(5, 5)
Print a DataFrame that contains boolean values that indicate whether the elements of new_data
are greater than 1.
pd.DataFrame(new_data > 1)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | True | True | True | False | False |
1 | False | False | False | False | True |
2 | False | False | False | True | False |
3 | True | False | False | False | False |
4 | False | False | False | True | False |
Print a NumPy array that contains only the elements of new_data
that are greater than 1.
new_data[new_data > 1]
array([2.3, 1.5, 1.6, 1.2, 2.2, 1.1, 1.7])