Python Foundation

+91-7982108418            
  1. Write a Pandas program to split the following dataframe into groups based on school code. Also check the type of GroupBy object.

Test Data:

schoolclassnamedate_Of_Birthageheightweightaddress
S1     s001VAlberto Franco15/05/20021217335street1
S2     s002VGino Mcneill17/05/20021219232street2
S3     s003VIRyan Parkes16/02/19991318633street3
S4     s001VIEesha Hinton25/09/19981316730street1
S5     s002VGino Mcneill11/05/20021415131street2
S6     s004VIDavid Parkes15/09/19971215932street4
  • Write a Pandas program to split the following given dataframe by school code and get mean, min, and max value of age for each school.

Test Data:

 schoolclassnamedate_Of_Birthageheightweightaddress
S1s001VAlberto Franco15/05/20021217335street1
S2s002VGino Mcneill17/05/20021219232street2
S3s003VIRyan Parkes16/02/19991318633street3
S4s001VIEesha Hinton25/09/19981316730street1
S5s002VGino Mcneill11/05/20021415131street2
S6s004VIDavid Parkes15/09/19971215932street4
  • Write a Pandas program to split the following given dataframe into groups based on school code and class.

Test Data:

schoolclassnamedate_Of_Birthageheightweightaddress
S1     s001VAlberto Franco15/05/20021217335street1
S2     s002VGino Mcneill17/05/20021219232street2
S3     s003VIRyan Parkes16/02/19991318633street3
S4     s001VIEesha Hinton25/09/19981316730street1
S5     s002VGino Mcneill11/05/20021415131street2
S6     s004VIDavid Parkes15/09/19971215932street4
  • Write a Pandas program to split the following given dataframe into groups based on school code and cast grouping as a list.

Test Data:

school class            name date_Of_Birth   age height weight address S1          s001     V Alberto Franco     15/05/2002                     12          173    35 street1

S2   s002VGino Mcneill17/05/20021219232street2
S3   s003VIRyan Parkes16/02/19991318633street3
S4   s001VIEesha Hinton25/09/19981316730street1
S5   s002VGino Mcneill11/05/20021415131street2
S6   s004VIDavid Parkes15/09/19971215932street4
  • Write a Pandas program to split the following given dataframe into groups based on single column and multiple columns. Find the size of the grouped data.

Test Data:

schoolclassnamedate_Of_Birthageheightweightaddress
S1     s001VAlberto Franco15/05/20021217335street1
S2     s002VGino Mcneill17/05/20021219232street2
S3     s003VIRyan Parkes16/02/19991318633street3
S4     s001VIEesha Hinton25/09/19981316730street1
S5     s002VGino Mcneill11/05/20021415131street2
S6     s004VIDavid Parkes15/09/19971215932street4
  • Write a Pandas program to split the following given dataframe into groups based on school code and call a specific group with the name of the group.

Test Data:

schoolclassnamedate_Of_Birthageheightweightaddress
S1     s001VAlberto Franco15/05/20021217335street1
S2     s002VGino Mcneill17/05/20021219232street2
S3     s003VIRyan Parkes16/02/19991318633street3
S4     s001VIEesha Hinton25/09/19981316730street1
S5     s002VGino Mcneill11/05/20021415131street2
S6     s004VIDavid Parkes15/09/19971215932street4
  • Write a Pandas program to split a dataset, group by one column and get mean, min, and max values by group. Using the following dataset find the mean, min, and max values of purchase amount (purch_amt) group by customer id (customer_id).

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 2012-10-05   3005      5002

1    70009     270.65 2012-09-10         3001         5005

2    70002      65.26 2012-10-05         3002         5001

3    70004     110.50 2012-08-17         3009         5003

4    70007     948.50 2012-09-10         3005         5002

5    70005    2400.60 2012-07-27         3007         5001

6    70008    5760.00 2012-09-10         3002         5001

7    70010    1983.43 2012-10-10         3004         5006

8    70003    2480.40 2012-10-10         3009         5003

9    70012     250.45 2012-06-27         3008         5002

10   70011      75.29 2012-08-17         3003         5007

11   70013    3045.60 2012-04-25         3002         5001

  • Write a Pandas program to split a dataset to group by two columns and count by each row.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 2012-10-05   3005      5002

170009270.652012-09-1030015005
27000265.262012-10-0530025001
370004110.502012-08-1730095003
470007948.502012-09-1030055002
5700052400.602012-07-2730075001
6700085760.002012-09-1030025001
7700101983.432012-10-1030045006
8700032480.402012-10-1030095003
970012250.452012-06-2730085002
107001175.292012-08-1730035007
11700133045.602012-04-2530025001
  • Write a Pandas program to split a dataset to group by two columns and then sort the aggregated results within the groups.

In the following dataset group on ‘customer_id’, ‘salesman_id’ and then sort sum of purch_amt within the groups.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 2012-10-05   3005      5002

1    70009     270.65 2012-09-10         3001         5005

2    70002      65.26 2012-10-05         3002         5001

3    70004     110.50 2012-08-17         3009         5003

4    70007     948.50 2012-09-10         3005         5002

5    70005    2400.60 2012-07-27         3007         5001

6    70008    5760.00 2012-09-10         3002         5001

7    70010    1983.43 2012-10-10         3004         5006

8    70003    2480.40 2012-10-10         3009         5003

9    70012     250.45 2012-06-27         3008         5002

10   70011      75.29 2012-08-17         3003         5007

11   70013    3045.60 2012-04-25         3002         5001

  1. Write a Pandas program to split the following dataframe into groups based on customer id and create a list of order date for each group.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 2012-10-05   3005      5002

1    70009     270.65 2012-09-10         3001         5005

2    70002      65.26 2012-10-05         3002         5001

3    70004     110.50 2012-08-17         3009         5003

4    70007     948.50 2012-09-10         3005         5002

5    70005    2400.60 2012-07-27         3007         5001

6    70008    5760.00 2012-09-10         3002         5001

7    70010    1983.43 2012-10-10         3004         5006

8    70003    2480.40 2012-10-10         3009         5003

9    70012     250.45 2012-06-27         3008         5002

10   70011      75.29 2012-08-17         3003         5007

11   70013    3045.60 2012-04-25         3002         5001

  1. Write a Pandas program to split the following dataframe into groups and calculate monthly purchase amount.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 05-10-2012 3001      5002

1    70009     270.65 09-10-2012         3001         5005

2    70002      65.26 05-10-2012         3005         5001

3    70004     110.50 08-17-2012         3001         5003

4    70007     948.50 10-09-2012         3005         5002

5    70005    2400.60 07-27-2012         3001         5001

6    70008    5760.00 10-09-2012         3005         5001

7    70010    1983.43 10-10-2012         3001         5006

8    70003    2480.40 10-10-2012         3005         5003

9    70012     250.45 06-17-2012         3001         5002

10   70011      75.29 07-08-2012         3005         5007

11   70013    3045.60 04-25-2012         3005         5001

  1. Write a Pandas program to split the following dataframe into groups, group by month and year based on order date and find the total purchase amount year wise, month wise.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 05-10-2012 3001      5002

1    70009     270.65 09-10-2012         3001         5005

2    70002      65.26 05-10-2012         3005         5001

3    70004     110.50 08-17-2012         3001         5003

4    70007     948.50 10-09-2012         3005         5002

5    70005    2400.60 07-27-2012         3001         5001

6    70008    5760.00 10-09-2012         3005         5001

7    70010    1983.43 10-10-2012         3001         5006

8    70003    2480.40 10-10-2012         3005         5003

9    70012     250.45 06-17-2012         3001         5002

10   70011      75.29 07-08-2012         3005         5007

11   70013    3045.60 04-25-2012         3005         5001

  1. Write a Pandas program to split the following dataframe into groups based on first column and set other column values into a list of values.

Test Data:

X        Y   Z 0 10 10 22

1 10 15 20

2 10 11 18

3 20 20 20

4 30 21 13

5 30 12 10

6 10 14   0

  1. Write a Pandas program to split the following dataframe into groups based on all columns and calculate Groupby value counts on the dataframe.

id type book 0   1 10 Math 1   2 15 English 2   1 11 Physics 3   1 20 Math 4   2 21 English 5   1 12 Physics 6   2 14 English Output:     book English Math Physics 1 10 0 1 0   11 0 0 1   12 0 0 1   20 0 1 0 2 14 1 0 0   15 1 0 0   21 1 0 0     Test Data:

id type

  1. Write a Pandas program to split the following dataframe into groups and count unique values of ‘value’ column.

Test Data:

id value

  • 1 a
  • 1 a
  • 2 b
  • 3 None
  • 3 a
  • 4 a
  • 4 None
  • 4 b

Output: value

a 3

b 2

  1. Write a Pandas program to split a given dataframe into groups and list all the keys from the GroupBy object.

Test Data:

 school_codeclassnamedate_Of_Birthageheightweight
S1s001VAlberto Franco15/05/20021217335
S2s002VGino Mcneill17/05/20021219232
S3s003VIRyan Parkes16/02/19991318633
S4s001VIEesha Hinton25/09/19981316730
S5s002VGino Mcneill11/05/20021415131

S6        s004    VI    David Parkes     15/09/1997   12     159  32

  1. Write a Pandas program to split a given dataframe into groups and create a new column with count from GroupBy.

Test Data:

book_name book_type book_id

  • Book1      Math        1
  • Book2   Physics        2
  • Book3 Computer        3
  • Book4   Science        4
  • Book1      Math        1
  • Book2   Physics        2
  • Book3 Computer        3
  • Book5   English        5
  • Write a Pandas program to split a given dataframe into groups with bin counts.

Test Data:

ord_no purch_amt customer_id sales_id 0     70001     150.50     3005      5002

1    70009     270.65         3001      5003

2    70002      65.26         3002      5004

3    70004     110.50         3009      5003

4    70007     948.50         3005      5002

5    70005    2400.60         3007      5001

6    70008    5760.00         3002      5005

7    70010    1983.43         3004      5007

8    70003    2480.40         3009      5008

9    70012     250.45         3008      5004

10   70011      75.29         3003      5005

11   70013    3045.60         3002      5001

  1. Write a Pandas program to split a given dataframe into groups with multiple aggregations.

Split the following given dataframe by school code, class and get mean, min, and max value of height and age for each value of the school.

Test Data:

school class            name date_Of_Birth   age height                        weight address

S1s001VAlberto Franco15/05/20021217335street1
S2s002VGino Mcneill17/05/20021219232street2
S3s003VIRyan Parkes16/02/19991318633street3
S4s001VIEesha Hinton25/09/19981316730street1
S5s002VGino Mcneill11/05/20021415131street2
S6s004VIDavid Parkes15/09/19971215932street4
  • Write a Pandas program to split a given dataframe into groups and display target column as a list of unique values.

Test Data:

id type     book

  • A     1     Math
  • A     1     Math
  • A     1 English
  • A     1 Physics
  • A     2     Math
  • A     2 English
  • B     1 Physics
  • B     1 English
  • B     1 Physics
  • B     2 English
  • B     2 English

Output:

List all unique values in a group: id type       book

  • A     1 Math,English,Physics
  • A     2          Math,English
  • B     1       Physics,English
  • B     2               English
  • Write a Pandas program to split the following dataframe into groups and calculate quarterly purchase amount.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 05-10-2012 3001      5002

1    70009     270.65 09-10-2012         3001         5005

2    70002      65.26 05-10-2012         3005         5001

3    70004     110.50 08-17-2012         3001         5003

4    70007     948.50 10-09-2012         3005         5002

5    70005    2400.60 07-27-2012         3001         5001

6    70008    5760.00 10-09-2012         3005         5001

7    70010    1983.43 10-10-2012         3001         5006

8    70003    2480.40 10-10-2012         3005         5003

9    70012     250.45 06-17-2012         3001         5002

10   70011      75.29 07-08-2012         3005         5007

11   70013    3045.60 04-25-2012         3005         5001

  • Write a Pandas program to split the following given dataframe into groups by school code and get mean, min, and max value of age with customized column name for each school.

Test Data:

school class            name date_Of_Birth   age height                        weight address

S1   s001VAlberto Franco15/05/2002   12    17335 street1
S2   s002VGino Mcneill17/05/2002   12    19232 street2
S3s003VIRyan Parkes16/02/19991318633 street3
S4s001VIEesha Hinton25/09/19981316730 street1
S5s002VGino Mcneill11/05/20021415131 street2
S6s004VIDavid Parkes15/09/19971215932 street4
  • Write a Pandas program to split the following given datasets into groups on customer id and calculate the number of customers starting with ‘C’, the list of all products and the difference of maximum purchase amount and minimum purchase amount.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 05-10-2012 C3001     5002

1    70009     270.65 09-10-2012       C3001         5005

2    70002      65.26 05-10-2012       D3005         5001

3    70004     110.50 08-17-2012       D3001         5003

4    70007     948.50 10-09-2012       C3005         5002

5    70005    2400.60 07-27-2012       D3001         5001

6    70008    5760.00 10-09-2012       C3005         5001

7    70010    1983.43 10-10-2012       D3001         5006

8    70003    2480.40 10-10-2012       D3005         5003

9    70012     250.45 06-17-2012       C3001         5002

10   70011      75.29 07-08-2012       D3005         5007

11   70013    3045.60 04-25-2012       D3005         5001

  • Write a Pandas program to split the following given datasets into groups on customer_id to summarize purch_amt and calculate percentage of purch_amt in each group.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 05-10-2012 3001      5002

1    70009     270.65 09-10-2012         3001         5005

2    70002      65.26 05-10-2012         3005         5001

3    70004     110.50 08-17-2012         3001         5003

4    70007     948.50 10-09-2012         3005         5002

5    70005    2400.60 07-27-2012         3001         5001

6    70008    5760.00 10-09-2012         3005         5001

7    70010    1983.43 10-10-2012         3001         5006

8    70003    2480.40 10-10-2012         3005         5003

9    70012     250.45 06-17-2012         3001         5002

10   70011      75.29 07-08-2012         3005         5007

11   70013    3045.60 04-25-2012         3005         5001

  • Write a Pandas program to split a dataset, group by one column and get mean, min, and max values by group, also change the column name of the aggregated metric. Using the following dataset find the mean, min, and max values of purchase amount (purch_amt) group by customer id (customer_id).

Test Data:

school class            name date_Of_Birth   age height     weight address S1      s001     V Alberto Franco  15/05/2002              12           173                  35 street1

S2s002VGino Mcneill17/05/20021219232street2
S3s003VIRyan Parkes16/02/19991318633street3
S4s001VIEesha Hinton25/09/19981316730street1
S5s002VGino Mcneill11/05/20021415131street2
S6s004VIDavid Parkes15/09/19971215932street4
  • Write a Pandas program to split a given dataset, group by two columns and convert other columns of the dataframe into a dictionary with column header as key.

Test Data:

school class            name date_Of_Birth   age height     weight address S1      s001                 V Alberto Franco     15/05/2002     12     173                  35 street1 S2     s002     V    Gino Mcneill     17/05/2002   12    192         32 street2 S3 s003   VI                   Ryan Parkes     16/02/1999     13     186                  33 street3 S4     s001    VI    Eesha Hinton     25/09/1998   13    167         30 street1 S5 s002            V   Gino Mcneill     11/05/2002 14   151      31 street2 S6 s004   VI   David Parkes     15/09/1997 12   159      32 street4

  • Write a Pandas program to split a given dataset, group by one column and apply an aggregate function to few columns and another aggregate function to the rest of the columns of the dataframe.
  \salesman_idsale_jansale_febsale_marsale_aprsale_maysale_jun
05002150.50250.50150.50150.50130.50150.50
15005270.65170.65270.65270.65270.65270.65
2500165.2615.2665.2695.2665.2645.26
35003110.50110.50110.50210.50310.50110.50
45002948.50598.50948.50948.50948.50948.50
550012400.601400.602400.602400.602400.603400.60
650011760.002760.005760.00760.00760.005760.00
750062983.431983.431983.431983.431983.43983.43
85003480.402480.402480.402480.402480.402480.40
950021250.45250.45250.45250.45250.45250.45
10500775.2975.2975.2975.2975.2975.29
1150011045.603045.603045.603045.603045.603045.60
 sale_julsale_augsale_sepsale_octsale_novsale_dec 
0950.50150.50150.50150.50150.50150.50 
1270.6570.65270.65270.65270.6570.65 
265.2665.2665.2665.2695.2665.26 
3210.50110.50110.50110.50110.50110.50 
4948.50948.50948.50948.50948.50948.50 
52400.60400.60200.602400.602400.602400.60 
65760.005760.005760.005760.005760.005760.00 

7     983.43   1983.43   1983.43   1983.43   1983.43   1983.43

8    2480.40   2480.40   2480.40   2480.40   2480.40   2480.40

9     250.45    250.45    250.45    250.45    250.45    250.45

10     75.29     75.29     75.29     75.29     75.29     75.29

11   3045.60   3045.60   3045.60   3045.60   3045.60   3045.60

  • Write a Pandas program to split a given dataset, group by one column and remove those groups if all the values of a specific columns are not available.

Test Data:

schoolclassnamedate_Of_Birthageheightweight address
S1     s001VAlberto Franco15/05/20021217335 street1
S2     s002VGino Mcneill17/05/20021219232 street2
S3     s003VIRyan Parkes16/02/19991318633 street3
S4     s001VIEesha Hinton25/09/19981316730 street1
S5     s002VGino Mcneill11/05/20021415131 street2
S6     s004VIDavid Parkes15/09/19971215932 street4
  • Write a Pandas program to split a given dataset using group by on specified column into two labels and ranges.

Split the group on ‘salesman_id’, Ranges:

1) (5001…5006)

2) (5007..5012)

Test Data:

salesman_id sale_jan 0          5001        150.50

1          5002    270.65

2          5003     65.26

3          5004    110.50

4          5005    948.50

5          5006   2400.60

6          5007   1760.00

7          5008   2983.43

8          5009    480.40

9          5010   1250.45

10         5011     75.29

11         5012   1045.60

  • Write a Pandas program to split the following dataset using group by on first column and aggregate over multiple lists on second column.

Test Data:

student_idmarks
0       S001[88, 89, 90]
1       S001[78, 81, 60]
2       S002[84, 83, 91]
3       S002[84, 88, 91]
4       S003[90, 89, 92]
5       S003[88, 59, 90]

Output: student_id

S001 [83.0, 85.0, 75.0]

S002 [84.0, 85.5, 91.0]

S003 [89.0, 74.0, 91.0]

  • Write a Pandas program to split the following dataset using group by on ‘salesman_id’ and find the first order date for each group.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 2012-10-05   3005      5002

1    70009     270.65 2012-09-10         3001         5005

2    70002      65.26 2012-10-05         3002         5001

3    70004     110.50 2012-08-17         3009         5003

4    70007     948.50 2012-09-10         3005         5002

5    70005    2400.60 2012-07-27         3007         5001

6    70008    5760.00 2012-09-10         3002         5001

7    70010    1983.43 2012-10-10         3004         5004

8    70003    2480.40 2012-10-10         3009         5003

9    70012     250.45 2012-06-27         3008         5002

10   70011      75.29 2012-08-17         3003         5004

11   70013    3045.60 2012-04-25         3002         5001

  • Write a Pandas program to split a given dataset using group by on multiple columns and drop last n rows of from each group.

Test Data:

ord_no purch_amt    ord_date customer_id salesman_id 0   70001     150.50 2012-10-05   3002      5002

1    70009     270.65 2012-09-10         3001         5003

2    70002      65.26 2012-10-05         3001         5001

3    70004     110.50 2012-08-17         3003         5003

4    70007     948.50 2012-09-10         3002         5002

5    70005    2400.60 2012-07-27         3002         5001

6    70008    5760.00 2012-09-10         3001         5001

7    70010    1983.43 2012-10-10         3004         5003

8    70003    2480.40 2012-10-10         3003         5003

9    70012     250.45 2012-06-27         3002         5002

10   70011      75.29 2012-08-17         3003         5003

11   70013    3045.60 2012-04-25         3001         5001

Powered by BetterDocs

Contact Us
close slider

error: Content is protected !!