i need help with some homework. they want me to make a 10x10 multiplication tables using multiple while loops and nesting. they want the user to be prompted for the first number for the row and column. so if the you give 3 for column and 12 for the row it would look like this:
3 4 5 6 7 8 9 10 11 12
--------------------------------------------------
12| 36 48 60 72 84 96 108 120 132 144
13| 39 52 65 78 91 104 117 130 143 156
14| 42 56 70 84 98 112 126 140 154 168
15| 45 60 75 90 105 120 135 150 165 180
16| 48 64 80 96 112 128 144 160 176 192
17| 51 68 85 102 119 136 153 170 187 204
18| 54 72 90 108 126 144 162 180 198 216
19| 57 76 95 114 133 152 171 190 209 228
20| 60 80 100 120 140 160 180 200 220 240
21| 63 84 105 126 147 168 189 210 231 252
this is what i found with the internet search help:
row = int(input("Enter the first row number: " ))
while(row <= 10):
column = int(input("Enter the frist column number: "))
while(column <= 10):
if(row+column==0):
print('{:4s}'.format(''),end = '') #corner
elif(row*column==0):
print('{:4d}'.format(row+column),end = '') # border
else:
print('{:4d}'.format(row*column),end = '') # table
column=column+1
print()
row=row+1
if anyone could help me i would be very thankful
It should look something more like this:
row1 = int(input("Enter the first row number: " ))
column1 = int(input("Enter the first column number: "))
# TODO: print header
for row in range(row1, row1 + 10):
for column in range(column1, column1 + 10):
# TODO
That is, you only prompt for input twice, not (1+N) times, and you use the built-in function range() to generate the lists of rows and columns to iterate over.
Related
I'm trying to merge 2 date columns (contains blanks cells) and extract the non-duplicates excluding the blank cells while sorted in ascending order.
However, I'm encountering error trying to do so. As excel took in the blank cells as data and extracted data in the end contains invalid info that were not found in either columns. The extracted data were not sort correctly too.
S1 Date
S1 Count
S2 Date
S2 Count
Overall Date
Overall Count
01/10/2021
56
01/10/2021
127
01/10/2021
183
02/10/2021
98
02/10/2021
125
02/10/2021
223
03/10/2021
122
03/10/2021
51
03/10/2021
173
04/10/2021
45
04/10/2021
54
04/10/2021
99
05/10/2021
81
05/10/2021
64
05/10/2021
145
06/10/2021
21
06/10/2021
87
06/10/2021
108
07/10/2021
84
07/10/2021
63
07/10/2021
147
08/10/2021
12
08/10/2021
79
08/10/2021
91
09/10/2021
63
09/10/2021
45
09/10/2021
108
10/10/2021
79
10/10/2021
81
10/10/2021
160
11/10/2021
65
11/10/2021
21
11/10/2021
86
12/10/2021
81
12/10/2021
121
12/10/2021
202
20/10/2021
71
14/10/2021
54
20/10/2021
204
21/10/2021
83
15/10/2021
97
21/10/2021
151
22/10/2021
127
16/10/2021
67
22/10/2021
205
23/10/2021
84
17/10/2021
98
23/10/2021
137
24/10/2021
121
18/10/2021
54
24/10/2021
144
25/10/2021
54
19/10/2021
62
25/10/2021
54
26/10/2021
58
20/10/2021
133
26/10/2021
58
27/10/2021
87
21/10/2021
68
27/10/2021
87
28/10/2021
64
22/10/2021
78
28/10/2021
102
29/10/2021
34
23/10/2021
53
29/10/2021
109
30/10/2021
120
24/10/2021
23
30/10/2021
184
31/10/2021
78
28/10/2021
38
31/10/2021
78
29/10/2021
75
00/01/1900
0
30/10/2021
64
14/10/2021
54
15/10/2021
97
16/10/2021
67
17/10/2021
98
18/10/2021
54
19/10/2021
62
Excel image- Error highlighted in yellow
The formula used was =SORT(IFERROR(IFERROR(INDEX(DateS1,MATCH(0,COUNTIF($F$3:F3,DateS1),0)),INDEX(DateS2,MATCH(0,COUNTIF($F$3:F3,DateS2),0))),""))
Really appreciate if anyone could help.
If you have Windows Excel, I suggest a different formula:
Data Range: G1:J100
L1: overallDate
L2: =SORT(UNIQUE(FILTERXML("<t><s>"&TEXTJOIN("</s><s>",TRUE,INDEX(G2:I100,SEQUENCE(99),{1,3}))&"</s></t>","//s")))
M2: =IF(L2="","", SUMIF($G$1:$G$100,L2,$H$1:$H$100)+SUMIF($I$1:$I$100,L2,$J$1:$J$100))
Results from L2 will SPILL down as far as needed
In L2 adjust the range reference and SEQUENCE count as appropriate for your data, or make them dynamic
Formula in M2 will need to be copied down
Edit: If you want an inclusive list of dates in the OverallDate column, consider:
L2: =LET(dates,INDEX(G2:I100,SEQUENCE(99),{1,3}),MIN(dates)-1 + SEQUENCE(1+MAX(dates)-MIN(dates)))
Let's assume we have a vector and matrix like below:
r =: 100 + 5 5 $ i.25
r
100 101 102 103 104
105 106 107 108 109
110 111 112 113 114
115 116 117 118 119
120 121 122 123 124
v =: 100 + 5 $ i.5
v
100 101 102 103 104
Now I would like to have a way to interchange fragments as specified by selectors.
I know how I can exchange items:
(<0 _1) &C. v
104 101 102 103 100
here I interchanged element at index=0 and index=-1.
In case of matrix, the rows (items) are changed:
(<0 _1) &C. r
120 121 122 123 124
105 106 107 108 109
110 111 112 113 114
115 116 117 118 119
100 101 102 103 104
But what about if I want to specify two arbitrary selections. Example to what I am after:
sel1 =: (< (<0 1))
sel1 { v
100 101
sel2 =: (< (<2 3))
sel2 { v
102 103
sel1 sel2 INTERCHANGE v
102 103 100 101 104
And the same for matrix:
sel1 =: (< (<0 1),(<0 1))
sel1 { r
100 101
105 106
sel2 =: (< (<3 4),(<1 2))
sel2 { r
116 117
121 122
sel1 sel2 INTERCHANGE r
116 117 102 103 104
121 122 107 108 109
110 111 112 113 114
115 100 101 118 119
120 105 106 123 124
So my question would be how to define elegantly interchange that uses two selections?
I think that I would first create the two selections and then use Amend to swap them. May not be the most elegant or generalizable, but if you know the selections that you want to change and they are the same shape, it does work.
r
100 101 102 103 104
105 106 107 108 109
110 111 112 113 114
115 116 117 118 119
120 121 122 123 124
[rep=:((<3 4;1 2),(<0 1;0 1)) { r NB. rep is the selected replacement values
116 117
121 122
100 101
105 106
((<0 1;0 1),(<3 4;1 2)){ r NB. values that will be replaced (just a check that they are the same shape)
100 101
105 106
116 117
121 122
rep ((<0 1;0 1),(<3 4;1 2))} r NB. Select verb ({) changed to Amend adverb (})
116 117 102 103 104
121 122 107 108 109
110 111 112 113 114
115 100 101 118 119
120 105 106 123 124
I have a really strange problem and don't know how to solve it.
I am using Ubuntu 18.04.2 together with Python 3.7.3 64-bit and use VScode as an editor.
I am reading data from a database and write it to a csv file with csv.writer
import pandas as pd
import csv
with open(raw_path + station + ".csv", "w+") as f:
file = csv.writer(f)
# Write header into csv
colnames = [par for par in param]
file.writerow(colnames)
# Write data into csv
for row in data:
file.writerow(row)
This works perfectly fine, it provides a .csv file with all the data I read from the database up to the current timestep. However in a later working step I have to read this data to a pandas dataframe and merge it with another pandas dataframe. I read the files like this:
data1 = pd.read_csv(raw_path + file1, sep=',')
data2 = pd.read_csv(raw_path + file2, sep=',')
And then merge the data like this:
comb_data = pd.merge(data1, data2, on="datumsec", how="left").fillna(value=-999)
For 5 out of 6 locations that I do this, everything works perfectly fine, the combined dataset has the same length as the two seperate ones. However for one location pd.read_csv seems not to read the csv files properly. I checked whether the problem is already in the database readout but everything is OK there, I can open both files with sublime and they have the same length, however when I read them with pandas.read_csv one shows less lines. The best part is, this problem is appearing totally random. Sometimes it works and reads the entire file, sometimes not. AND it occures at different locations in the file. Sometimes it stops after approx. 20000 entries, sometimes at 45000, sometimes somewhere else.. just totally random.
Here is an overview of my test output when I print all the lengths of the files
print(len(data1)): 57105
print(len(data2)): 57105
both values directly after read out from database, before writing it anywhere..
After saving the data as csv as described above and opening it in excel or sublime or anything I can confirm that the data contains 57105 rows. Everything is where it is supposed to be.
However if I try to read the data as with pd.read_csv
print(len(data1)): 48612
print(len(data2)): 57105
both values after reading in the data from the csv file
data1 48612
datumsec tl rf ff dd ffx
0 1538352000 46 81 75 288 89
1 1538352600 47 79 78 284 93
2 1538353200 45 82 79 282 93
3 1538353800 44 84 71 284 91
4 1538354400 43 86 77 288 96
5 1538355000 43 85 78 289 91
6 1538355600 46 80 79 286 84
7 1538356200 51 72 68 285 83
8 1538356800 52 71 68 281 73
9 1538357400 48 75 68 276 80
10 1538358000 45 78 62 271 76
11 1538358600 42 82 66 273 76
12 1538359200 43 81 70 274 78
13 1538359800 44 80 68 275 78
14 1538360400 45 78 66 279 72
15 1538361000 45 78 67 282 73
16 1538361600 43 79 63 275 71
17 1538362200 43 81 69 280 74
18 1538362800 42 80 70 281 76
19 1538363400 43 78 69 285 77
20 1538364000 43 78 71 285 77
21 1538364600 44 75 61 288 71
22 1538365200 45 73 56 290 62
23 1538365800 45 72 44 297 57
24 1538366400 44 73 51 286 57
25 1538367000 43 76 61 281 70
26 1538367600 40 79 66 284 73
27 1538368200 39 78 70 291 76
28 1538368800 38 80 71 287 81
29 1538369400 36 81 74 285 81
... ... .. ... .. ... ...
48582 1567738800 7 100 0 210 0
48583 1567739400 6 100 0 210 0
48584 1567740000 5 100 0 210 0
48585 1567740600 6 100 0 210 0
48586 1567741200 4 100 0 210 0
48587 1567741800 4 100 0 210 0
48588 1567742400 5 100 0 210 0
48589 1567743000 4 100 0 210 0
48590 1567743600 4 100 0 210 0
48591 1567744200 4 100 0 209 0
48592 1567744800 4 100 0 209 0
48593 1567745400 5 100 0 210 0
48594 1567746000 6 100 0 210 0
48595 1567746600 5 100 0 210 0
48596 1567747200 5 100 0 210 0
48597 1567747800 5 100 0 210 0
48598 1567748400 5 100 0 210 0
48599 1567749000 6 100 0 210 0
48600 1567749600 6 100 0 210 0
48601 1567750200 5 100 0 210 0
48602 1567750800 4 100 0 210 0
48603 1567751400 5 100 0 210 0
48604 1567752000 6 100 0 210 0
48605 1567752600 7 100 0 210 0
48606 1567753200 6 100 0 210 0
48607 1567753800 5 100 0 210 0
48608 1567754400 6 100 0 210 0
48609 1567755000 7 100 0 210 0
48610 1567755600 7 100 0 210 0
48611 1567756200 7 100 0 210 0
[48612 rows x 6 columns]
datumsec tl rf schnee ival6
0 1538352000 115 61 25 107
1 1538352600 115 61 25 107
2 1538353200 115 61 25 107
3 1538353800 115 61 25 107
4 1538354400 115 61 25 107
5 1538355000 115 61 25 107
6 1538355600 115 61 25 107
7 1538356200 115 61 25 107
8 1538356800 115 61 25 107
9 1538357400 115 61 25 107
10 1538358000 115 61 25 107
11 1538358600 115 61 25 107
12 1538359200 115 61 25 107
13 1538359800 115 61 25 107
14 1538360400 115 61 25 107
15 1538361000 115 61 25 107
16 1538361600 115 61 25 107
17 1538362200 115 61 25 107
18 1538362800 115 61 25 107
19 1538363400 115 61 25 107
20 1538364000 115 61 25 107
21 1538364600 115 61 25 107
22 1538365200 115 61 25 107
23 1538365800 115 61 25 107
24 1538366400 115 61 25 107
25 1538367000 115 61 25 107
26 1538367600 115 61 25 107
27 1538368200 115 61 25 107
28 1538368800 115 61 25 107
29 1538369400 115 61 25 107
... ... ... ... ... ...
57075 1572947400 -23 100 -2 -999
57076 1572948000 -23 100 -2 -999
57077 1572948600 -22 100 -2 -999
57078 1572949200 -23 100 -2 -999
57079 1572949800 -24 100 -2 -999
57080 1572950400 -23 100 -2 -999
57081 1572951000 -21 100 -1 -999
57082 1572951600 -21 100 -1 -999
57083 1572952200 -23 100 -1 -999
57084 1572952800 -23 100 -1 -999
57085 1572953400 -22 100 -1 -999
57086 1572954000 -23 100 -1 -999
57087 1572954600 -22 100 -1 -999
57088 1572955200 -24 100 0 -999
57089 1572955800 -24 100 0 -999
57090 1572956400 -25 100 0 -999
57091 1572957000 -26 100 -1 -999
57092 1572957600 -26 100 -1 -999
57093 1572958200 -27 100 -1 -999
57094 1572958800 -25 100 -1 -999
57095 1572959400 -27 100 -1 -999
57096 1572960000 -29 100 -1 -999
57097 1572960600 -28 100 -1 -999
57098 1572961200 -28 100 -1 -999
57099 1572961800 -27 100 -1 -999
57100 1572962400 -29 100 -2 -999
57101 1572963000 -29 100 -2 -999
57102 1572963600 -29 100 -2 -999
57103 1572964200 -30 100 -2 -999
57104 1572964800 -28 100 -2 -999
[57105 rows x 5 columns]
To me there is no obvious reason in the data why it should have problems reading the entire file and obviously there are none, considering that sometimes it reads the entire file and sometimes not.
I am really clueless about this. Do you have any idea how to cope with that and what could be the problem?
I finally solved my problem and as expected it was not within the file itself. I am using multiprocesses to run the named functions and some other things in parallel. The reading from database + writing to csv file and reading from csv file are performed in two different processes. Therefore the second process (reading from csv) did not know that the csv file was still being written and read only what was already available in the csv file. Because the file was opened by a different process it did not throw an exception when being opened.
I thought I already took care of this but obviously not thoroughly enough, excluding every possible case.
I had completely the same problem with a different application and also did not understand what was wrong, because sometimes it worked and sometimes it didn't.
In a for loop, I was extracting the last two rows of a dataframe that I was creating in the same file. Sometimes, the extracted rows where not the last two at all, but most of the times it worked fine. I guess the program started extracting the last two rows before the writing process was done.
I paused the script for half a second to make sure the writing process is done:
import time
time.sleep(0.5)
However, I don't think this is not a very elegant solution, since it might not be sufficient if somebody with a slower computer uses the script for instance.
Vroni, how did you solve this in the end, is there a way to define that a specific process must not be processed parallel with other tasks. I did not define anything about parallel processing in my program, so I think if this is the cause it is done automatically.
I have a dataframe that looks similar to below:
Wave A B C
340 77 70 15
341 80 73 15
342 83 76 16
343 86 78 17
I want to generate columns that will have all the possible combinations of the existing columns. I showed 3 cols here but in my actual data, I have 7 columns and therefore 127 total combinations. The desired output is as follows:
Wave A B C AB AC AD BC ... ABC
340 77 70 15 147 92 ...
341 80 73 15 153 95 ...
342 83 76 16 159 99 ...
I implemented a quite inefficient version where the user inputs the combinations (AB, AC, etc.) and a new col is created with the sum of the rows. This seems almost impossible to accomplish for 127 combinations, esp with descriptive col names.
Create a list of all combinations with chain + combinations from itertools, then sum the appropriate columns:
from itertools import combinations, chain
cols = [*df.iloc[:,1:]]
l = list(chain.from_iterable(combinations(cols, n+2) for n in range(len(cols))))
#[('A', 'B'), ('A', 'C'), ('B', 'C'), ('A', 'B', 'C')]
for items in l:
df[''.join(items)] = df.loc[:, items].sum(1)
Wave A B C AB AC BC ABC
0 340 77 70 15 147 92 85 162
1 341 80 73 15 153 95 88 168
2 342 83 76 16 159 99 92 175
3 343 86 78 17 164 103 95 181
You need to get the all combination first , then we just get the combination , and we need create the maps dict or Series
l=df.columns[1:].tolist()
l1=[list(map(list, itertools.combinations(l, i))) for i in range(len(l) + 1)]
d=[dict.fromkeys(y,''.join(y))for x in l1 for y in x ]
maps=pd.Series(d).apply(pd.Series).stack()
df.set_index('Wave',inplace=True)
df=df.reindex(columns=maps.index.get_level_values(1))
#here using reindex , get the order of your new df to the maps keys
df.columns=maps.tolist()
# here assign the new value to the column , since the order is same that why here I am assign it back
df.sum(level=0,axis=1)
Out[303]:
A B C AB AC BC ABC
Wave
340 77 70 15 147 92 85 162
341 80 73 15 153 95 88 168
342 83 76 16 159 99 92 175
343 86 78 17 164 103 95 181
I have the following df,
code y_m count
101 2017-11 86
101 2017-12 32
102 2017-11 11
102 2017-12 34
102 2018-01 46
103 2017-11 56
103 2017-12 89
now I want to convert this df into a matrix that transposes column y_m to row, make the count as matrix cell values like,
0 1 2 3 4
0 -1 0 2017-11 2017-12 2018-01
1 0 354 153 155 46
2 101 118 86 32 -1
3 102 91 11 34 46
4 103 145 -1 89 -1
in specific, -1 represents a dummy value that indicates either a value doesn't exist for a y_m for a specific code or to maintain matrix shape; 0 represents 'all' values, that aggregates code or y_m or code and y_m, e.g. cell (1, 1) sums the count values for all y_m and code; (1,2) sums the count for 2017-11.
You can use first pivot_table:
df1 = (df.pivot_table(index='code',
columns='y_m',
values='count',
margins=True,
aggfunc='sum',
fill_value=-1,
margins_name='0'))
print (df1)
y_m 2017-11 2017-12 2018-01 0
code
101 86 32 -1 118
102 11 34 46 91
103 56 89 -1 145
0 153 155 46 354
And then for final format, but get mixed values, numeric with strings:
#change order of index and columns values for reindex
idx = df1.index[-1:].tolist() + df1.index[:-1].tolist()
cols = df1.columns[-1:].tolist() + df1.columns[:-1].tolist()
df2 = (df1.reindex(index=idx, columns=cols)
.reset_index()
.rename(columns={'code':-1})
.rename_axis(None,1))
#add columns to first row
df3 = df2.columns.to_frame().T.append(df2).reset_index(drop=True)
#reset columns names to range
df3.columns = range(len(df3.columns))
print (df3)
0 1 2 3 4
0 -1 0 2017-11 2017-12 2018-01
1 0 354 153 155 46
2 101 118 86 32 -1
3 102 91 11 34 46
4 103 145 56 89 -1