Can you tell me how to make a combined distribution with Python?

Asked 2 years ago, Updated 2 years ago, 17 views

Can you tell me how to make a combined distribution with Python? I don't know how to do it in a piece, but I don't know how to combine it.

For example, given raw data, the row should be height (150-160; 160-170; 170-180;) and heat should be divided into weight (40-50; 50-60, 60-70) to create a combined distribution table I would appreciate it if you could let me know the code, but if it's difficult, it would be very helpful if you could suggest the tools you need to use.

P.S. Do you know how to set different intervals between categories when dividing ranges? When I googled, I found that the category spacing was constant, otherwise I couldn't find a way to set it up individually. ex : Key (above 150 to 155, less than 155 to 165)

python

2022-09-20 19:56

1 Answers

>>> import random
>>> random.normalvariate(173, 4)
169.80165874433067
>>> key = [random.normalvariate (171, 7) for _ in range (30)]
>>> Weight = [random.normalvariate (68, 10) for _in range (30)]
>>> Key
[163.56879430116317, 162.22173001725076, 176.00801254009994, 169.5507386679232, 169.39525006222004, 170.88315341736174, 169.09580745969214, 178.68189526927057, 170.09094441473383, 190.50342799606022, 164.05186167935977, 170.12520735368116, 166.92195871597255, 180.8569156017258, 173.048064865071, 171.59817670156855, 174.3197143994516, 158.85336291561825, 164.9219727414754, 164.51300891361328, 162.64808453949905, 161.6232751682463, 173.53714194566567, 175.77100898262185, 171.69845332557077, 163.27962146586515, 166.09477553602366, 170.89070908439362, 176.8148119692912, 159.61334199452318]
>>> Weight
[66.99290478451694, 82.83895592563472, 60.21868655318631, 67.54483320099003, 63.77046404838144, 74.69508772698411, 54.03291854797147, 74.65317492061382, 70.5834380726465, 71.40588442831626, 66.03558459738376, 83.50826980677327, 80.84686226202994, 68.94690486095789, 58.97192764923703, 66.50390166196242, 51.012037009307235, 61.646362063997664, 62.05592175650585, 58.160756245032445, 53.9787973380094, 68.12608828472912, 68.680553390887, 67.4302957009136, 73.37638329263656, 49.26440845923726, 64.2172223808422, 61.85748349784684, 52.43992689488411, 69.92132323763535]
>>> import pandas as pd
>>> 
>>> data = pd.DataFrame ({'height':height, 'weight':weight})
>>> data
             Height and weight
0   163.568794  66.992905
1   162.221730  82.838956
2   176.008013  60.218687
3   169.550739  67.544833
4   169.395250  63.770464
5   170.883153  74.695088
6   169.095807  54.032919
7   178.681895  74.653175
8   170.090944  70.583438
9   190.503428  71.405884
10  164.051862  66.035585
11  170.125207  83.508270
12  166.921959  80.846862
13  180.856916  68.946905
14  173.048065  58.971928
15  171.598177  66.503902
16  174.319714  51.012037
17  158.853363  61.646362
18  164.921973  62.055922
19  164.513009  58.160756
20  162.648085  53.978797
21  161.623275  68.126088
22  173.537142  68.680553
23  175.771009  67.430296
24  171.698453  73.376383
25  163.279621  49.264408
26  166.094776  64.217222
27  170.890709  61.857483
28  176.814812  52.439927
29  159.613342  69.921323
>>> data['key10'] = data['key']//10*10
>>> data["Weight"] = data["Weight"]//10*10
>>> data
             Height, weight. Height, weight, weight
0   163.568794  66.992905  160.0   60.0
1   162.221730  82.838956  160.0   80.0
2   176.008013  60.218687  170.0   60.0
3   169.550739  67.544833  160.0   60.0
4   169.395250  63.770464  160.0   60.0
5   170.883153  74.695088  170.0   70.0
6   169.095807  54.032919  160.0   50.0
7   178.681895  74.653175  170.0   70.0
8   170.090944  70.583438  170.0   70.0
9   190.503428  71.405884  190.0   70.0
10  164.051862  66.035585  160.0   60.0
11  170.125207  83.508270  170.0   80.0
12  166.921959  80.846862  160.0   80.0
13  180.856916  68.946905  180.0   60.0
14  173.048065  58.971928  170.0   50.0
15  171.598177  66.503902  170.0   60.0
16  174.319714  51.012037  170.0   50.0
17  158.853363  61.646362  150.0   60.0
18  164.921973  62.055922  160.0   60.0
19  164.513009  58.160756  160.0   50.0
20  162.648085  53.978797  160.0   50.0
21  161.623275  68.126088  160.0   60.0
22  173.537142  68.680553  170.0   60.0
23  175.771009  67.430296  170.0   60.0
24  171.698453  73.376383  170.0   70.0
25  163.279621  49.264408  160.0   40.0
26  166.094776  64.217222  160.0   60.0
27  170.890709  61.857483  170.0   60.0
28  176.814812  52.439927  170.0   50.0
29  159.613342  69.921323  150.0   60.0
>>> data.groupby ('height 10', 'weight 10').size().unstack()
Weight 1040.050.060.070.080.0
Height 10                                
150.0   NaN   NaN   2.0   NaN   NaN
160.0   1.0   3.0   7.0   NaN   2.0
170.0   NaN   3.0   5.0   4.0   1.0
180.0   NaN   NaN   1.0   NaN   NaN
190.0   NaN   NaN   NaN   1.0   NaN
>>> data.groupby ('height 10', 'weight 10').size().unstack().fillna(0)
Weight 1040.050.060.070.080.0
Height 10                                
150.0   0.0   0.0   2.0   0.0   0.0
160.0   1.0   3.0   7.0   0.0   2.0
170.0   0.0   3.0   5.0   4.0   1.0
180.0   0.0   0.0   1.0   0.0   0.0
190.0   0.0   0.0   0.0   1.0   0.0


2022-09-20 19:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.