In: Computer Science
Python.
5) What will the code below do? (Assume that we have a dataset df with these two columns named Occupation' and 'Age')
df.groupby('Occupation')['Age'].mean()
a) It will return the average age per occupation
b) It will return an error
c) It will return the total age per occupation
d) None of the options
6) df.describe() will return basic descriptive statistics only for numerical variables
True/False ?
7) Pandas dataframes can be converted into numpy arrays
Truse/False ?
Ans 5.)
The correct option is a) It will return the average age
per occupation. (Attaching example reference)
Ans 6.)
The correct option is False. df.describe() can return statistics for other data types as well. (Attaching example reference)
Ans 7.)
The correct option is True. You can convert a dataframe to numpy arrays.
To convert a dataframe into a NumPy array you can use df.values. Add .values() with the rename_axis() function and you will get the desired result.
Below is the code for the same:-
import numpy as np
import pandas as pd
#defining index
index = [1, 2, 3, 4, 5, 6, 7]
#creating the rows
a = [np.nan, 0.1, np.nan, 0.1, 0.4, 0.1, 0.1]
b = [0.4, np.nan, 0.2, 0.2, 0.2, np.nan, np.nan]
c = [np.nan, 0.4, 0.5, np.nan, 0.6, 0.3, np.nan]
#defining the dataframe
df = pd.DataFrame({'A': a, 'B': b, 'C': c}, index=index)
#converting it to np arrays
df = df.rename_axis('ID').values
print(df)