Python code is required to calculate statistics based on data from the DataFrame Pivot. Interested in best practice of writing code.

For each pivot column, it is required to calculate the number of nonzero values ​​and the arithmetic mean of nonzero values. From the obtained results, select TOP (4) values ​​with the largest number of nonzero values ​​and the arithmetic average value.

Source code (returns data to pivot)

import pandas as pd import argparse as ap import numpy as np from itertools import combinations from prettytable import PrettyTable from collections import Counter import json from random import randint from itertools import chain import operator from functools import reduce def ArgParser(): parser = ap.ArgumentParser(description='Description of your program') parser.add_argument('-d','--draws', help='Test result', required=True) args = vars(parser.parse_args()) return args def main(): args = ArgParser() draws = pd.read_csv(args['draws'], sep=';',header=0, index_col='draw_id') bc = (draws.stack().value_counts()).to_frame(name='count') bc['category'] = np.arange(len(bc)) // 3 res = (draws.stack() .map(bc['category']) .reset_index(name='category') .pivot_table(index='draw_id', columns='category', aggfunc='size', fill_value=0)) print(bc) print(res) print('The End') if __name__ == '__main__': main() 

Expected result on the example of the first 10 rows from the pivot table

enter image description here

TOP marked in green (4)

enter image description here

Intermediate data (part) from Pivot

 category 0 1 2 3 4 5 6 7 8 9 10 11 draw_id 1 0 0 2 0 1 1 1 0 1 0 0 1 2 1 0 1 1 1 1 1 0 0 1 0 0 3 0 0 2 0 1 0 0 0 0 2 1 1 4 1 1 1 0 0 1 0 1 1 0 0 1 5 0 2 0 0 1 1 0 1 1 0 0 1 6 1 2 1 0 0 0 0 1 1 0 1 0 7 0 0 1 1 2 0 0 0 2 0 0 1 8 0 1 0 1 0 0 1 1 1 1 1 0 9 1 0 1 0 0 1 1 1 1 0 0 1 10 1 1 0 1 1 0 0 1 0 1 1 0 11 0 0 2 0 0 1 1 0 0 2 0 1 12 1 1 1 0 0 0 1 0 1 0 0 2 13 1 0 0 2 1 2 0 1 0 0 0 0 14 1 0 0 1 0 1 1 0 2 0 1 0 15 0 2 0 0 1 1 1 1 0 1 0 0 16 1 0 0 1 2 1 1 0 0 0 1 0 17 1 1 0 1 0 0 0 2 0 0 2 0 18 1 1 1 0 1 1 1 0 1 0 0 0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1 0 0 1 1 1 0 0 0 101 0 0 2 1 1 0 1 1 0 0 1 0 102 0 0 2 0 0 0 2 1 0 0 1 1 103 0 1 0 1 1 1 1 0 1 1 0 0 [103 rows x 12 columns] 

Initial data

 draw_id;ball1;ball2;ball3;ball4;ball5;ball6;ball7 1;9;11;17;29;30;33;36 2;5;6;9;11;15;33;28 3;4;5;10;14;20;30;8 4;4;21;22;25;26;36;6 5;1;11;13;17;24;29;36 6;2;17;22;24;27;30;1 7;4;15;26;28;29;35;34 8;12;22;24;26;31;33;2 9;6;9;16;24;33;34;17 10;3;8;12;19;27;31;1 11;4;5;7;9;14;20;25 12;9;20;32;33;35;36;22 13;16;19;23;25;29;32;12 14;6;10;11;17;33;35;15 15;1;5;16;19;22;28;33 16;3;6;7;8;16;29;12 17;1;3;10;19;24;32;23 18;9;25;27;29;33;35;22 19;7;13;17;18;21;34;6 20;7;14;18;20;27;33;28 21;1;3;5;8;22;23;25 22;4;5;13;19;28;34;11 23;3;24;26;32;35;36;14 24;2;7;18;22;30;32;4 25;5;22;28;30;31;33;3 26;1;4;6;28;31;32;13 27;7;10;15;18;23;30;8 28;6;10;12;16;18;19;4 29;9;12;16;21;23;27;3 30;6;15;18;19;25;29;2 31;4;7;22;28;29;30;15 32;3;7;14;18;33;35;29 33;4;14;21;23;28;29;30 34;2;5;9;21;26;27;20 35;1;3;9;11;13;17;27 36;11;13;15;28;32;35;18 37;3;11;16;21;28;35;15 38;1;2;12;13;14;15;6 39;5;10;13;16;18;21;20 40;1;4;18;23;32;36;7 41;6;9;13;17;18;35;23 42;11;13;19;23;24;27;12 43;2;3;8;14;32;35;25 44;1;5;12;14;21;25;9 45;2;4;15;25;28;31;9 46;19;21;23;26;30;35;2 47;11;16;22;23;24;30;8 48;8;9;11;27;30;35;32 49;1;15;22;26;31;32;25 50;3;5;8;12;16;21;7 51;7;8;13;21;22;32;30 52;1;4;8;14;27;30;12 53;2;16;20;22;27;30;21 54;1;5;16;25;27;36;22 55;4;26;28;30;32;33;6 56;5;10;13;18;24;27;29 57;1;4;5;8;22;25;28 58;3;11;24;27;29;34;17 59;1;2;6;9;14;23;32 60;5;10;12;15;24;33;22 61;5;12;15;23;24;32;18 62;2;5;11;13;25;28;10 63;8;13;18;19;27;31;12 64;7;9;11;23;32;34;22 65;2;4;7;15;27;30;26 66;4;12;15;16;17;19;11 67;6;7;9;12;17;19;27 68;12;17;25;27;32;33;36 69;6;19;20;26;27;35;21 70;6;15;21;32;34;35;1 71;4;7;9;10;14;23;26 72;6;21;26;30;31;32;28 73;3;8;13;22;29;31;12 74;6;8;13;15;27;34;5 75;8;13;24;29;31;33;6 76;6;14;17;23;26;32;21 77;4;9;15;16;23;32;13 78;1;8;13;23;27;33;24 79;1;13;16;26;29;32;6 80;9;12;16;22;24;35;32 81;8;9;18;19;23;28;20 82;1;6;7;20;28;29;32 83;8;17;22;25;26;31;11 84;3;11;18;24;26;29;1 85;10;11;13;21;27;30;6 86;2;13;23;25;30;35;29 87;14;16;17;25;29;30;4 88;7;8;12;22;25;31;16 89;16;23;24;27;32;33;19 90;6;16;18;25;27;28;7 91;13;15;17;19;24;32;6 92;1;12;15;19;27;34;36 93;3;6;7;9;18;33;31 94;6;11;12;14;21;29;23 95;8;11;14;15;29;36;22 96;1;9;14;25;30;32;26 97;4;10;25;31;35;36;17 98;9;16;27;32;35;36;23 99;10;21;28;29;31;34;17 100;1;12;13;24;26;33;22 101;2;4;15;18;19;30;8 102;4;7;10;19;30;33;34 103;1;11;15;18;28;31;26 

    1 answer 1

    creating pivot-table from DF draws :

     bc = (draws.stack().value_counts()).to_frame(name='count') bc['category'] = np.arange(len(bc)) // 3 pvt = (draws.stack() .map(bc['category']) .reset_index(name='category') .pivot_table(index='draw_id', columns='category', aggfunc='size')) 

    pivot-table statistics:

     st = pvt.mean().to_frame(name='mean') st['nonzero'] = (pvt > 0).sum() res = st.nlargest(4, ['nonzero']) 

    result:

     In [30]: res Out[30]: mean nonzero category 0 1.285714 63 1 1.280702 57 3 1.210526 57 4 1.203704 54 
    • mean value must be greater than or equal to one. When calculating the arithmetic average, we take non-zero entries (only games in which a given category was present). By definition, the smallest number is category = 1, and the average cannot be less than 1. - Yuriy Tigiev
    • @YuriyTigiev, remove the parameter fill_value=0 - MaxU