This is for a Non-profit Hockey League. We have an excel program that looks through the divisions to make sure we have the best possible combination of teams at each level. For example for 43 teams and 5 divisions the possible combinations are (8,8,9,9,9) and (7,9,9,9,9). We have multiple divisions with different combinations?
#CodeCamper - Here is my datasheet based on 46 teams and 6 divisions I do not have a current formula. We list all of the possible options manually. We do divisions of no less than 6 teams and no more than 9 teams. I am looking for a way to populate this table with the data automatically based on specific conditions. For example the number of teams in total and the number of divisions.
Thank you for your help
Combination #
Div 1
Div 2
Div 3
Div 4
Div 5
Div 6
1
7
7
8
8
8
8
2
7
8
7
8
8
8
3
7
8
8
7
8
8
4
7
8
8
8
7
8
5
7
8
8
8
8
7
6
8
7
7
8
8
8
7
8
7
8
7
8
8
8
8
7
8
8
7
8
9
8
7
8
8
8
7
10
8
8
7
7
8
8
11
8
8
7
8
7
8
12
8
8
7
8
8
7
13
8
8
8
7
7
8
14
8
8
8
7
8
7
15
8
8
8
8
7
7
16
9
9
7
7
7
7
17
9
7
9
7
7
7
18
9
7
7
9
7
7
19
9
7
7
7
9
7
20
9
7
7
7
7
9
21
7
9
9
7
7
7
22
7
9
7
9
7
7
23
7
9
7
7
9
7
24
7
9
7
7
7
9
25
7
7
9
9
7
7
26
7
7
9
7
9
7
27
7
7
9
7
7
9
28
7
7
7
9
9
7
29
7
7
7
9
7
9
30
7
7
7
7
9
9
31
9
8
8
7
7
7
32
8
9
8
7
7
7
33
8
8
9
7
7
7
34
8
8
7
9
7
7
35
8
8
7
7
9
7
36
8
8
7
7
7
9
37
9
8
7
8
7
7
38
8
9
7
8
7
7
39
8
7
9
8
7
7
40
8
7
8
9
7
7
41
8
7
8
7
9
7
42
8
7
8
7
7
9
43
9
8
7
7
8
7
44
8
9
7
7
8
7
45
8
7
9
7
8
7
46
8
7
7
9
8
7
47
8
7
7
8
9
7
48
8
7
7
8
7
9
49
9
8
7
7
7
8
50
8
9
7
7
7
8
51
8
7
9
7
7
8
52
8
7
7
9
7
8
53
8
7
7
7
9
8
54
8
7
7
7
8
9
55
9
7
8
7
7
8
56
7
9
8
7
7
8
57
7
8
9
7
7
8
58
7
8
7
9
7
8
59
7
8
7
7
9
8
60
7
8
7
7
8
9
61
9
7
7
8
7
8
62
7
9
7
8
7
8
63
7
7
9
8
7
8
64
7
7
8
9
7
8
65
7
7
8
7
9
8
66
7
7
8
7
8
9
67
9
7
7
7
8
8
68
7
9
7
7
8
8
69
7
7
9
7
8
8
70
7
7
7
9
8
8
71
7
7
7
8
9
8
72
7
7
7
8
8
9
73
9
7
8
8
7
7
74
7
9
8
8
7
7
75
7
8
9
8
7
7
76
7
8
8
9
7
7
77
7
8
8
7
9
7
78
7
8
8
7
7
9
79
9
7
7
8
8
7
80
7
9
7
8
8
7
81
7
7
9
8
8
7
82
7
7
8
9
8
7
83
7
7
8
8
9
7
84
7
7
8
8
7
9
85
9
7
8
7
8
7
86
7
9
8
7
8
7
87
7
8
9
7
8
7
88
7
8
7
9
8
7
89
7
8
7
8
9
7
90
7
8
7
8
7
9
Related
I have csv file contain on 6 columns like this:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
I need to convert this columns to rows to be like this:
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7
8 8 8 8 8 8
How can do that please?
This is input
This is the output
Try:
import csv
with open("input.csv", "r") as f_in, open("output.csv", "w") as f_out:
reader = csv.reader(f_in, delimiter=" ")
writer = csv.writer(f_out, delimiter=" ")
writer.writerows(zip(*reader))
Contents of input.csv:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
Contents of output.csv after the script run:
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7
8 8 8 8 8 8
you are looking for a table pivot method
if you are using pandas , this will do the trick https://pandas.pydata.org/docs/reference/api/pandas.pivot_table.html
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Output digits to staircase table. Python
Hi everyone!
I'm start learning python (Nested Loops)
I'm write the following code:
for i in range(9, 2, -1):
x = 2
for j in range(i):
print(x, end=' ')
x = x+1
print()
It's output is:
2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9
2 3 4 5 6 7 8
2 3 4 5 6 7
2 3 4 5 6
2 3 4 5
2 3 4
2 3
But I need this output:
2 3 4 5 6 7 8 9 10
3 4 5 6 7 8 9 10
4 5 6 7 8 9 10
5 6 7 8 9 10
....
9 10
What I should change in my code to receive correct output? Please advice
for i in range (2,10):
for j in range (i,11):
print(j, end=' ')
print()
produces this output:
2 3 4 5 6 7 8 9 10
3 4 5 6 7 8 9 10
4 5 6 7 8 9 10
5 6 7 8 9 10
6 7 8 9 10
7 8 9 10
8 9 10
9 10
I downloaded a dataset in .csv format from kaggle which is about lego. There's a "Ages" column like this:
df['Ages'].unique()
array(['6-12', '12+', '7-12', '10+', '5-12', '8-12', '4-7', '4-99', '4+',
'9-12', '16+', '14+', '9-14', '7-14', '8-14', '6+', '2-5', '1½-3',
'1½-5', '9+', '5-8', '10-21', '8+', '6-14', '5+', '10-16', '10-14',
'11-16', '12-16', '9-16', '7+'], dtype=object)
These categories are the suggested ages for using and playing with the legos.
I'm intended to do some statistical analysis with these age bins. For example, I want to check the mean of these suggested ages.
However, since the type of each of them is string:
type(lego_dataset.loc[0]['Ages'])
str
I don't know how to work on the data.
I've already check How to categorize a range of values in Pandas DataFrame
But imagine there are 100 unique bins. It's not reasonable to prepare a list of 100 labels for each category. There should be a better way.
Not entirely sure what output you are looking for. See if the below code & output helps you.
df['Lage'] = df['Ages'].str.split('[-+]').str[0]
df['Uage'] = df['Ages'].str.split('[-+]').str[-1]
or
df['Lage'] = df['Ages'].str.extract('(\d+)', expand=True) #you don't get the fractions for row 17 & 18
df['Uage'] = df['Ages'].str.split('[-+]').str[-1]
Input
Ages
0 6-12
1 12+
2 7-12
3 10+
4 5-12
5 8-12
6 4-7
7 4-99
8 4+
9 9-12
10 16+
11 14+
12 9-14
13 7-14
14 8-14
15 6+
16 2-5
17 1½-3
18 1½-5
19 9+
20 5-8
21 10-21
22 8+
23 6-14
24 5+
25 10-16
26 10-14
27 11-16
28 12-16
29 9-16
30 7+
Output1
Ages Lage Uage
0 6-12 6 12
1 12+ 12
2 7-12 7 12
3 10+ 10
4 5-12 5 12
5 8-12 8 12
6 4-7 4 7
7 4-99 4 99
8 4+ 4
9 9-12 9 12
10 16+ 16
11 14+ 14
12 9-14 9 14
13 7-14 7 14
14 8-14 8 14
15 6+ 6
16 2-5 2 5
17 1½-3 1½ 3
18 1½-5 1½ 5
19 9+ 9
20 5-8 5 8
21 10-21 10 21
22 8+ 8
23 6-14 6 14
24 5+ 5
25 10-16 10 16
26 10-14 10 14
27 11-16 11 16
28 12-16 12 16
29 9-16 9 16
30 7+ 7
Output2
Ages Lage Uage
0 6-12 6 12
1 12+ 12
2 7-12 7 12
3 10+ 10
4 5-12 5 12
5 8-12 8 12
6 4-7 4 7
7 4-99 4 99
8 4+ 4
9 9-12 9 12
10 16+ 16
11 14+ 14
12 9-14 9 14
13 7-14 7 14
14 8-14 8 14
15 6+ 6
16 2-5 2 5
17 1½-3 1 3
18 1½-5 1 5
19 9+ 9
20 5-8 5 8
21 10-21 10 21
22 8+ 8
23 6-14 6 14
24 5+ 5
25 10-16 10 16
26 10-14 10 14
27 11-16 11 16
28 12-16 12 16
29 9-16 9 16
30 7+ 7
I have the following data:
at_score atp_1 atp_2 atp_3 g_date g_id g_time ht_diff ht_score htp_1 htp_2 htp_3
0 0 6 7 8 11/16/18 1 0 0 0 1 2 3
1 13 6 7 9 11/16/18 1 15 2 15 1 2 3
2 20 7 8 10 11/16/18 1 18 2 22 3 4 5
3 40 7 8 6 11/16/18 1 33 5 45 4 1 2
4 65 8 7 6 11/16/18 1 60 -3 62 1 2 3
5 0 6 7 8 11/20/18 2 0 0 0 1 2 3
6 10 9 7 8 11/20/18 2 7 -4 6 4 2 3
7 26 6 10 7 11/20/18 2 24 -1 25 1 5 4
8 40 9 7 8 11/20/18 2 42 5 45 1 2 5
9 65 6 7 10 11/20/18 2 60 5 70 1 5 2
where at_score, ht_score are the away & home team's score on a particular date (g_date), in a particular game (g_id), & at a particular time in the game (g_time). ht_diff represents the home team's score differential (ht_score - at_score). Finally, and for my purposes most importantly, atp_1, atp_2, atp_3 are the 3 away players who are playing at that point. htp_1, htp_2, htp_3 are their home team counterparts.
What I'd like to calculate is the variance-covariance matrix for each of the home & away team players based on how the ht_diff, ht_score & at_score changed while they were playing and the players they were playing with. For example away player 6 played with players 7 & 8 for the first 13 minutes of g_id 1 (ht_diff = 2 for this period) & the last 27 minutes (ht_diff = -3).
In the end I have about 2.5 million observations (as well as 10 players playing at a time) so finding a 'easy' to calculate this would be extremely helpful.
I am trying to divide a set of three numbers from a string. here is my code:
tline =fgetl(fid);
in_points=fgetl(fid);
B = strrep(in_points,' ',' ')
C = char(strsplit(B));
points = reshape(str2num(C), 3, [])'
My input file looks like this:
output 1 for p=0.01
8 8 1 4 15 1 5 17 1 17 17 1 13 1 2 10 3 2 16 4 2 18 6 2 6 3 3 9 3 3 9 7 3 2 13 3 7 18 3 19 20 3 12 4 4 1 6 4 12 10 5 9 12 5 8 19 5 18 4 6 13 9 6 12 16 6 6 8 7 17 12 7 18 6 8 7 15 8 8 8 9 3 19 9 17 19 9 20 2 10 20 4 10 3 8 10 11 7 11 10 12 11 4 14 11 19 3 12 4 11 12 6 11 12 11 13 12 19 14 12 13 15 12 14 18 12 3 19 12 1 3 13 9 9 13 20 10 13 5 13 13 4 17 13 15 16 14 11 18 14 20 3 15 6 13 15 7 16 15 12 17 15 9 1 16 11 1 16 9 5 16 11 12 16 11 16 16 20 19 16 19 13 17 16 16 17 5 19 17 19 1 18 20 10 18 13 16 18 6 1 19 16 4 19 20 7 19 13 11 19 2 19 19 1 6 20 10 14 20 16 15 20 18 16 20 7 20 20
I want to separate the numbers as
8 8 1
4 15 1
5 17 1
and so on. When I run this code in octave it shows error. Any help will be appreciated.
You're code seems generally fine to me, although as mentioned in Hoki's comment there's probably a cleaner way to do it.
The only error is that you never actually read in the first line of the data. The first fgetl command reads in your title line. The second reads the blank line between the title and your data, instead of what you probably wanted to be in_points
If you add in another fgetl between the tline and in_points lines, it worked for me.
>> points
points =
8 8 1
4 15 1
5 17 1
17 17 1
13 1 2
10 3 2
16 4 2
18 6 2
6 3 3
9 3 3
9 7 3
...
As Hoki mentioned, the B = strrep(in_points,' ',' ') line does nothing but replace a space with a space. not sure what you were trying to do there.