Python Pandas Data Aggregation Frescoplay Answers and Handson

Python Pandas Data Aggregation Frescoplay Answers Handson


Python Pandas Data Aggregation Frescoplay Answers and Hands-on:

 1.Which of the following methods is used to group data of a data frame, based on specific columns?

  1. groupby
  2. aggregate
  3. group
  4. groupat

Answer: 1)groupby

2.What does the expression df.iloc[:, lambda x : [0,3]] do? Consider a data frame df with columns ['A', 'B', 'C', 'D'] and rows ['r1', 'r2', 'r3'].

  1. Selects Column 'A' and 'C'
  2. Results in Error
  3. Selects Columns 'A', 'B', and 'C'
  4. Selects Column 'A' and 'D'

Answer: 4)Selects Column 'A' and 'D'

3.Consider a data frame df with 10 rows and index [ 'r1', 'r2', 'r3', 'row4', 'row5', 'row6', 'r7', 'r8', 'r9', 'row10']. What does the expression g = df.groupby(df.index.str.len()) do?

  1. Groups df based on index values
  2. Groups df based on length of each index value
  3. Groups df based on index strings
  4. Data frames cannot be grouped by index values. Hence it results in Error.

Answer: 4)Data frames cannot be grouped by index values. Hence it results in Error.

4.Consider a data frame df with columns ['A', 'B', 'C', 'D'] and rows ['r1', 'r2', 'r3'], Which of the following expression is used to extract columns 'C' and 'D'?

  1. df.loc[:, lambda x : x.columns.isin(['C', 'D'])]
  2. df[:, lambda x : x.columns.isin(['C', 'D'])]
  3. lambda x : x.columns.isin(['C', 'D'])
  4. None

Answer: 1)df.loc[:, lambda x : x.columns.isin(['C', 'D'])]

5.Which of the following method can be applied on a groupby object to get the group details?

  1. group_details
  2. groups
  3. get_groups
  4. fetch_groups

Answer: 2)groups

6.Consider a data frame df with 10 rows and index [ 'r1', 'r2', 'r3', 'row4', 'row5', 'row6', 'r7', 'r8', 'r9', 'row10']. How many rows are obtained after executing the below expressions

 g = df.groupby(df.index.str.len())

g.filter(lambda x: len(x) > 1)

  1. 9
  2. 1
  3. 5
  4. 10

Answer: 1)9

7.Consider a data frame df with columns ['A', 'B', 'C', 'D'] and rows ['r1', 'r2', 'r3']. What does the expression df[lambda x : x.index.str.endswith('3')] do?

  1. Returns the row name r3
  2. Results in Error
  3. Returns the third column
  4. Filters the row labelled r3

Answer: 4)Filters the row labelled r3

8.Consider a data frame df with columns ['A', 'B', 'C', 'D'] and rows ['r1', 'r2', 'r3']. Which of the following expression filters the rows whose column B values are greater than 45 and column 'C' values are less than 30?

  1. df.loc[(df.B > 45) & (df.C < 30)]
  2. df[df.B > 45 & df.C < 30]
  3. df.loc[df.B > 45 & df.C < 30]
  4. (df.B > 45) & (df.C < 30)

Answer: 1)df.loc[(df.B > 45) & (df.C < 30)]

9.Consider a data frame df with columns ['A', 'B', 'C', 'D'] and rows ['r1', 'r2', 'r3']. Which of the following expression filters the rows whose column B values are greater than 45?

  1. df.iloc[df.B > 45]
  2. df.B > 45
  3. df[df.B > 45]
  4. df.loc[B > 45]

Answer: 3)df[df.B > 45]

10.Consider a data frame df with 10 rows and index [ 'r1', 'r2', 'r3', 'row4', 'row5', 'row6', 'r7', 'r8', 'r9', 'row10']. What does the aggregate method shown in below code do?

 g = df.groupby(df.index.str.len())

g.aggregate({'A':len, 'B':np.sum})

  1. Computes Sum of column A values
  2. Computes length of column A
  3. Computes length of column A and Sum of Column B values of each group
  4. Computes length of column A and Sum of Column B values

Answer: 3)Computes length of column A and Sum of Column B values of each group

0/Post a Comment/Comments

#Advertisement

Top Post Ad