Top 5 Results per Subject - excel

I have a list of exam subjects, student numbers and the score of each student per subject. What I'm trying to do is identify the top 4 scoring students per subject.
An example of the data that I have currently:
+--------------------+-------------+------------+
| Subject | Student ID | Test Score |
+--------------------+-------------+------------+
| Maths 1 | Student 001 | 78 |
| Maths 2 | Student 001 | 63 |
| English Lang | Student 001 | 64 |
| English Lit | Student 001 | 88 |
| Chemistry | Student 001 | 89 |
| Biology | Student 001 | 81 |
| Physics | Student 001 | 42 |
| French | Student 001 | 73 |
| Spanish | Student 001 | 78 |
| German | Student 001 | 67 |
| Engineering | Student 001 | 38 |
| Design & Tech | Student 001 | 57 |
| ... | ... | ... |
| ICT | Student 002 | 95 |
| Physical Education | Student 002 | 73 |
| Law | Student 002 | 37 |
| Sociology | Student 002 | 66 |
| Art & Design | Student 002 | 40 |
| Drama | Student 002 | 51 |
| Dance | Student 002 | 75 |
| General Studies | Student 002 | 37 |
| Maths 1 | Student 003 | 56 |
| Maths 2 | Student 003 | 77 |
| English Lang | Student 003 | 41 |
| ... | ... | ... |
| Sociology | Student 003 | 48 |
| Art & Design | Student 003 | 79 |
| Drama | Student 003 | 61 |
| Dance | Student 003 | 96 |
| General Studies | Student 003 | 50 |
| Maths 1 | Student 004 | 45 |
| Maths 2 | Student 004 | 73 |
| English Lang | Student 004 | 40 |
| English Lit | Student 004 | 60 |
| Chemistry | Student 004 | 77 |
| Biology | Student 004 | 56 |
| Physics | Student 004 | 69 |
| French | Student 004 | 90 |
| ... | ... | ... |
+--------------------+-------------+------------+
So Far I've created a pivot table with the subject as the Row Label, Student ID as the Column Label and the Sum of Test Scores as the Values, which gives me something like this:
+--------------------+-------------+-------------+-----+-------------+-------------+
| Row Labels | Student 001 | Student 002 | ... | Student 097 | Student 098 |
+--------------------+-------------+-------------+-----+-------------+-------------+
| Art & Design | 88 | 85 | ... | 79 | 91 |
| Biology | 81 | 123 | ... | 70 | 56 |
| Business Studies | 64 | 124 | ... | 47 | 51 |
| Chemistry | 89 | 100 | ... | 41 | 77 |
| Dance | 67 | 170 | ... | 96 | 36 |
| Design & Tech | 57 | 131 | ... | 31 | 89 |
| Drama | 35 | 130 | ... | 61 | 61 |
| Engineering | 38 | 125 | ... | 38 | 58 |
| English Lang | 64 | 102 | ... | 41 | 40 |
| English Lit | 88 | 160 | ... | 81 | 60 |
| French | 73 | 92 | ... | 69 | 90 |
| General Studies | 37 | 125 | ... | 50 | 63 |
| Geography | 41 | 117 | ... | 89 | 93 |
| German | 67 | 77 | ... | 33 | 89 |
| History | 85 | 176 | ... | 82 | 79 |
| Home Economics | 89 | 155 | ... | 71 | 46 |
| ICT | 85 | 179 | ... | 82 | 63 |
| Law | 46 | 102 | ... | 72 | 68 |
| Maths 1 | 78 | 98 | ... | 56 | 45 |
| Maths 2 | 63 | 137 | ... | 77 | 73 |
| Physical Education | 82 | 138 | ... | 30 | 80 |
| Physics | 42 | 108 | ... | 40 | 69 |
| Religious Studies | 66 | 113 | ... | 95 | 73 |
| Sociology | 87 | 152 | ... | 48 | 59 |
| Spanish | 78 | 152 | ... | 70 | 50 |
+--------------------+-------------+-------------+-----+-------------+-------------+
I've then used the large function adjacent to the table to give me the top 5 scores per row. However, what I need is the top 5 students ID's with the corresponding scores, rather than just the scores.
Apologies for the long post, I hope this all makes sense?
UPDATE -
Ideally I need an output similar to the following:
+--------------------+------------+------------+------------+------------+------------+
| | Result 1 | Result 2 | Result 3 | Result 4 | Result 5 |
+--------------------+------------+------------+------------+------------+------------+
| Art & Design | Student 92 | Student 88 | Student 85 | Student 62 | Student 94 |
| Biology | Student 86 | Student 74 | Student 63 | Student 90 | Student 60 |
| Business Studies | Student 56 | Student 66 | Student 77 | Student 97 | Student 97 |
| Chemistry | Student 86 | Student 67 | Student 95 | Student 58 | Student 56 |
| Dance | Student 91 | Student 65 | Student 76 | Student 82 | Student 87 |
| Design & Tech | Student 57 | Student 60 | Student 91 | Student 85 | Student 92 |
| Drama | Student 52 | Student 91 | Student 67 | Student 80 | Student 81 |
| Engineering | Student 58 | Student 82 | Student 95 | Student 88 | Student 71 |
| English Lang | Student 87 | Student 69 | Student 53 | Student 63 | Student 71 |
| English Lit | Student 93 | Student 75 | Student 78 | Student 91 | Student 77 |
| French | Student 76 | Student 64 | Student 92 | Student 63 | Student 80 |
| General Studies | Student 53 | Student 95 | Student 97 | Student 51 | Student 82 |
| Geography | Student 70 | Student 77 | Student 54 | Student 75 | Student 64 |
| German | Student 90 | Student 50 | Student 60 | Student 73 | Student 92 |
| History | Student 75 | Student 61 | Student 78 | Student 64 | Student 92 |
| Home Economics | Student 89 | Student 75 | Student 92 | Student 60 | Student 70 |
| ICT | Student 51 | Student 53 | Student 54 | Student 87 | Student 97 |
| Law | Student 92 | Student 86 | Student 67 | Student 55 | Student 50 |
| Maths 1 | Student 64 | Student 88 | Student 50 | Student 71 | Student 91 |
| Maths 2 | Student 71 | Student 82 | Student 93 | Student 64 | Student 64 |
| Physical Education | Student 80 | Student 70 | Student 89 | Student 82 | Student 64 |
| Physics | Student 93 | Student 78 | Student 94 | Student 55 | Student 53 |
| Religious Studies | Student 92 | Student 80 | Student 70 | Student 57 | Student 63 |
| Sociology | Student 58 | Student 51 | Student 91 | Student 68 | Student 71 |
| Spanish | Student 88 | Student 93 | Student 79 | Student 71 | Student 76 |
+--------------------+------------+------------+------------+------------+------------+

Pivot table is a good option.
Put subject and student id (in that order) into the Row Labels field. Place Test Score into Values field.
You need to right click on any of the values in the second column > sort > largest to smallest

After your create the above pivot,
paste special only the values to a new blank excel sheet.
Now transpose it into a new or same sheet... (shortcut is alt e + s+ e) ( so now subjects would be columns and student no.s as rows)
Now sort on subject from large to small (you will have sort manually as many number of times as the number of subjects
Alternate:
Ignore the above Pivot you made
Create a new pivot table with student no. in rows, subjects in columns and sort on subjects from large to small

Related

Unpivoting data in excel that contains multiple (15) categories in columns using vba

I have code that unpivots columns into rows. There are 19 categories of data, 15 of which have been unpivoted. However, my problem is that some of the tables that are unpivoted are not showing up on the new rows. I am asking for anyone's expertise to help as this will be helpful for me in future endeavors. I have created a table. Bear in mind this time is extremely wide/long and has 131 columns I believe and only 7 rows. Below is the table of the original data (it is make believe data of course but will be used for real data in the future). Also, the 2nd table is the how I want it to look. The 3rd table is how I how it actually looks. Under that is my code. I will glady upvote anyone who will help me. Thank you in advance.
Original data:
| usr | Company | Dept.# | Dept1 | Dept2 | Dept3 | Dept4 | Hr1 | Tr1 | F1 | A1 | HOH1 | M1 | R1 | SO1 | BIG1 | T1 | P1 | X1 | Y1 | Z1 | Tin1 | Hr1 | Tr1 | F1 | A1 | HOH1 | M1 | R1 | SO1 | BIG1 | T1 | P1 | X1 | Y1 | Z1 | Tin1 | Hr1 | Tr1 | F1 | A1 | HOH1 | M1 | R1 | SO1 | BIG1 | T1 | P1 | X1 | Y1 | Z1 | Tin1 | Hr1 | Tr1 | F1 | A1 | HOH1 | M1 | R1 | SO1 | BIG1 | T1 | P1 | X1 | Y1 | Z1 | Tin1 | Hr2 | Tr2 | F2 | A2 | HOH2 | M2 | R2 | SO2 | BIG2 | T2 | P2 | X2 | Y2 | Z2 | Tin2 | Hr2 | Tr2 | F2 | A2 | HOH2 | M2 | R2 | SO2 | BIG2 | T2 | P2 | X2 | Y2 | Z2 | Tin2 | Hr2 | Tr2 | F2 | A2 | HOH2 | M2 | R2 | SO2 | BIG2 | T2 | P2 | X2 | Y2 | Z2 | Tin2 | Hr3 | Tr3 | F3 | A3 | HOH3 | M3 | R3 | SO3 | BIG3 | T3 | P3 | X2 | Y2 | Z2 | Tin2 | Hr3 | Tr3 | F3 | A3 | HOH3 | M3 | R3 | SO3 | BIG3 | T3 | P3 | X3 | Y3 | Z3 | Tin3 | Hr4 | Tr4 | F4 | A4 | HOH4 | M4 | R4 | SO4 | BIG4 | T4 | P4 | X4 | Y4 | Z4 | Tin4 |
|------|---------|--------|-------|-------|-------|-------|-----|-----|-----|-----|------|----|----|-----|------|----|-----|-----|-----|----|------|-----|-----|----|-----|------|----|-----|-----|------|----|-----|-----|-----|----|------|-----|-----|----|-----|------|----|----|-----|------|-----|----|----|----|-----|------|-----|-----|----|-----|------|----|----|-----|------|----|----|-----|-----|----|------|-----|-----|-----|-----|------|----|----|-----|------|----|----|-----|----|-----|------|-----|-----|----|-----|------|----|----|-----|------|----|----|----|----|-----|------|-----|-----|-----|-----|------|----|----|-----|------|----|----|----|-----|----|------|-----|-----|-----|-----|------|----|-----|-----|------|----|----|-----|-----|----|------|-----|-----|-----|-----|------|----|----|-----|------|----|----|-----|-----|-----|------|-----|-----|-----|-----|------|----|----|-----|------|----|-----|-----|-----|-----|------|
| xxxx | OS | 1 | Train | | | | 20 | 89 | 355 | 123 | 435 | 90 | 5 | 55 | 676 | 34 | 43 | 984 | 345 | 74 | 846 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| xxxx | OPC | 2 | Poxy1 | Poxy2 | | | | | | | | | | | | | | | | | | 45 | 546 | 68 | 345 | 903 | 70 | 345 | 23 | 54 | 32 | 234 | 23 | 567 | 69 | 64 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 38 | 67 | 235 | 789 | 7 | 40 | 99 | 98 | 87 | 89 | 34 | 312 | 42 | 756 | 23 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| xxxx | Oxy R | 4 | H1 | H2 | H3 | H4 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 22 | 36 | 13 | 678 | 64 | 40 | 34 | 239 | 76 | 87 | 34 | 999 | 965 | 34 | 93 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 89 | 54 | 761 | 765 | 9 | 20 | 22 | 65 | 78 | 98 | 78 | 75 | 354 | 23 | 23 | | | | | | | | | | | | | | | | 36 | 80 | 123 | 543 | 17 | 20 | 11 | 908 | 988 | 7 | 86 | 245 | 546 | 763 | 324 | 25 | 90 | 111 | 432 | 84 | 25 | 63 | 784 | 98 | 78 | 854 | 754 | 234 | 865 | 43 |
| xxxx | HPK | 3 | Test1 | Test2 | Test3 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 99 | 456 | 39 | 567 | 223 | 50 | 5 | 32 | 549 | 435 | 34 | 87 | 64 | 348 | 942 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 52 | 21 | 47 | 876 | 1 | 30 | 46 | 92 | 78 | 12 | 34 | 12 | 12 | 421 | 23 | | | | | | | | | | | | | | | | 90 | 76 | 773 | 654 | 49 | 10 | 223 | 982 | 566 | 23 | 54 | 786 | 356 | 73 | 654 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| xxxx | Mano | 1 | Porp | | | | 42 | 657 | 645 | 234 | 344 | 80 | 45 | 364 | 97 | 23 | 634 | 34 | 23 | 87 | 84 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| xxxx | Macro | 2 | Otto1 | Otto2 | | | | | | | | | | | | | | | | | | 75 | 574 | 46 | 456 | 453 | 60 | 44 | 235 | 867 | 5 | 433 | 234 | 346 | 46 | 35 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 23 | 433 | 186 | 987 | 2 | 30 | 34 | 58 | 87 | 43 | 34 | 23 | 62 | 73 | 32 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
How I want it to look:
| usr | Company | Dept# | Dept | Hrs | Tr | F | A | HOH | M | R | SO | BIG | T | P | X | Y | Z | Tin |
|------|---------|-------|-------|-----|-----|-----|-----|-----|----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
| xxxx | OS | 1 | Train | 20 | 89 | 355 | 123 | 435 | 90 | 5 | 55 | 676 | 34 | 43 | 984 | 345 | 74 | 846 |
| xxxx | OPC | 2 | Poxy1 | 45 | 546 | 68 | 345 | 903 | 70 | 345 | 23 | 54 | 32 | 234 | 23 | 567 | 69 | 64 |
| xxxx | OPC | 2 | Poxy2 | 38 | 67 | 235 | 789 | 7 | 40 | 99 | 98 | 87 | 89 | 34 | 312 | 42 | 756 | 23 |
| xxxx | Oxy R | 4 | H1 | 22 | 36 | 13 | 678 | 64 | 40 | 34 | 239 | 76 | 87 | 34 | 999 | 965 | 34 | 93 |
| xxxx | Oxy R | 4 | H2 | 89 | 54 | 761 | 765 | 9 | 20 | 22 | 65 | 78 | 98 | 78 | 75 | 354 | 23 | 23 |
| xxxx | Oxy R | 4 | H3 | 36 | 80 | 123 | 543 | 17 | 20 | 11 | 908 | 988 | 7 | 86 | 245 | 546 | 763 | 324 |
| xxxx | Oxy R | 4 | H4 | 25 | 90 | 111 | 432 | 84 | 25 | 63 | 784 | 98 | 78 | 854 | 754 | 234 | 865 | 43 |
| xxxx | HPK | 3 | Test1 | 99 | 456 | 39 | 567 | 223 | 50 | 5 | 32 | 549 | 435 | 34 | 87 | 64 | 348 | 942 |
| xxxx | HPK | 3 | Test2 | 52 | 21 | 47 | 876 | 1 | 30 | 46 | 92 | 78 | 12 | 34 | 12 | 12 | 421 | 23 |
| xxxx | HPK | 3 | Test3 | 90 | 76 | 773 | 654 | 49 | 10 | 223 | 982 | 566 | 23 | 54 | 786 | 356 | 73 | 654 |
| xxxx | Mano | 1 | Porp | 42 | 657 | 645 | 234 | 344 | 80 | 45 | 364 | 97 | 23 | 634 | 34 | 23 | 87 | 84 |
| xxxx | Macro | 2 | Otto1 | 73 | 574 | 46 | 456 | 453 | 60 | 44 | 235 | 867 | 5 | 433 | 234 | 346 | 46 | 35 |
| xxxx | Macro | 2 | Otto2 | 23 | 433 | 186 | 987 | 2 | 30 | 34 | 58 | 87 | 43 | 34 | 23 | 62 | 73 | 32 |
This is how it actually looks which is wrong. As you can see data is missing for some reason:
| usr | Company | Dept# | Dept | Hrs | Tr | F | A | HOH | M | R | SO | BIG | T | P | X | Y | Z | Tin |
|------|---------|-------|-------|-----|-----|-----|-----|-----|----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
| xxxx | OS | 1 | Train | 20 | 89 | 355 | 123 | 435 | 90 | 5 | 55 | 676 | 34 | 43 | 984 | 345 | 74 | 846 |
| xxxx | OPC | 2 | Poxy1 | 45 | 546 | 68 | 345 | 903 | 70 | 345 | 23 | 54 | 32 | 234 | 23 | 567 | 69 | 64 |
| xxxx | OPC | 2 | Poxy2 | 38 | 67 | 235 | 789 | 7 | 40 | 99 | 98 | 87 | 89 | 34 | 312 | 42 | 756 | 23 |
| xxxx | Oxy R | 4 | H1 | 22 | 36 | 13 | 678 | 64 | 40 | 34 | 239 | 76 | 87 | 34 | 999 | 965 | 34 | 93 |
| xxxx | Oxy R | 4 | H2 | 89 | 54 | 761 | 765 | 9 | 20 | 22 | 65 | 78 | 98 | 78 | 75 | 354 | 23 | 23 |
| xxxx | Oxy R | 4 | H3 | | | | | | | | | | | | | | | |
| xxxx | Oxy R | 4 | H4 | | | | | | | | | | | | | | | |
| xxxx | HPK | 3 | Test1 | 99 | 456 | 39 | 567 | 223 | 50 | 5 | 32 | 549 | 435 | 34 | 87 | 64 | 348 | 942 |
| xxxx | HPK | 3 | Test2 | 52 | 21 | 47 | 876 | 1 | 30 | 46 | 92 | 78 | 12 | 34 | 12 | 12 | 421 | 23 |
| xxxx | HPK | 3 | Test3 | | | | | | | | | | | | | | | |
| xxxx | Mano | 1 | Porp | 42 | 657 | 645 | 234 | 344 | 80 | 45 | 364 | 97 | 23 | 634 | 34 | 23 | 87 | 84 |
| xxxx | Macro | 2 | Otto1 | 73 | 574 | 46 | 456 | 453 | 60 | 44 | 235 | 867 | 5 | 433 | 234 | 346 | 46 | 35 |
| xxxx | Macro | 2 | Otto2 | 23 | 433 | 186 | 987 | 2 | 30 | 34 | 58 | 87 | 43 | 34 | 23 | 62 | 73 | 32 |
Here is my code:
Sub buttonclick()
Dim Ary As Variant, Nary As Variant, Cary As Variant
Dim r As Long, c As Long, nr As Long, cc As Long
Cary = Array("0853", 6898, 113128, 143143)
With Sheets("Sheet1")
Ary = .Range("A2:DM" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
End With
ReDim Nary(1 To UBound(Ary) * 4, 1 To 19)
For r = 1 To UBound(Ary)
For c = 4 To 7
If Ary(r, c) = "" Then Exit For
nr = nr + 1
Nary(nr, 1) = Ary(r, 1): Nary(nr, 2) = Ary(r, 2): Nary(nr, 3) = Ary(r, 3)
Nary(nr, 4) = Ary(r, c)
For cc = Left(Cary(c - 4), 2) To Right(Cary(c - 4), 2) Step 15
Nary(nr, 5) = Nary(nr, 5) & Ary(r, cc)
Nary(nr, 6) = Nary(nr, 6) & Ary(r, cc + 1)
Nary(nr, 7) = Nary(nr, 7) & Ary(r, cc + 2)
Nary(nr, 8) = Nary(nr, 8) & Ary(r, cc + 3)
Nary(nr, 9) = Nary(nr, 9) & Ary(r, cc + 4)
Nary(nr, 10) = Nary(nr, 10) & Ary(r, cc + 5)
Nary(nr, 11) = Nary(nr, 11) & Ary(r, cc + 6)
Nary(nr, 12) = Nary(nr, 12) & Ary(r, cc + 7)
Nary(nr, 13) = Nary(nr, 13) & Ary(r, cc + 8)
Nary(nr, 14) = Nary(nr, 14) & Ary(r, cc + 9)
Nary(nr, 15) = Nary(nr, 15) & Ary(r, cc + 10)
Nary(nr, 16) = Nary(nr, 16) & Ary(r, cc + 11)
Nary(nr, 17) = Nary(nr, 17) & Ary(r, cc + 12)
Nary(nr, 18) = Nary(nr, 18) & Ary(r, cc + 13)
Nary(nr, 19) = Nary(nr, 19) & Ary(r, cc + 14)
Next cc
Next c
Next r
With Sheets("Sheet2")
.UsedRange.ClearContents
.Range("A1").Resize(, 19).Value = Array("usr", "Company", "Dept.#", "Dept", "Hrs",
"Tr", "F", "A", "HOH", "M", "R", "SO", "BIG", _
"T", "P", "X", "Y", "Z", "Tin")
.Range("A2").Resize(nr, 19).Value = Nary
End With
End Sub

How to Add/Subtract columns based on a particular value in Pandas

I am trying to create a new column called Total_Order_Amount in Dataframe whose value will be based on the Orderstatus. ex if order status is shipped then Total_Order_Amount = item_price + Tax - item_Discount - Tax_Discount. If the order status is cancelled then Total_Order_Aount = item_price - item_Discount.
Input DataFrame
+----------+--------------+------------+------+---------------+--------------+
| Order id | Order Status | item price | Tax | item discount | tax discount |
+----------+--------------+------------+------+---------------+--------------+
| 1 | Shipped | 400 | 72 | 30 | 72 |
+----------+--------------+------------+------+---------------+--------------+
| 2 | cancelled | 200 | 36 | 5 | 0 |
+----------+--------------+------------+------+---------------+--------------+
| 3 | Shipped | 180 | 32.4 | 18 | 0 |
+----------+--------------+------------+------+---------------+--------------+
| 4 | cancelled | 600 | 108 | 50 | 108 |
+----------+--------------+------------+------+---------------+--------------+
| 5 | shipped | 500 | 90 | 25 | 90 |
+----------+--------------+------------+------+---------------+--------------+
| 6 | cancelled | 280 | 50.4 | 15 | 50.4 |
+----------+--------------+------------+------+---------------+--------------+
Final Output required.
+----------+--------------+------------+------+---------------+--------------+--------------------+
| Order id | Order Status | item price | Tax | item discount | tax discount | total order amount |
+----------+--------------+------------+------+---------------+--------------+--------------------+
| 1 | Shipped | 400 | 72 | 30 | 72 | 370 |
+----------+--------------+------------+------+---------------+--------------+--------------------+
| 2 | cancelled | 200 | 36 | 5 | 0 | 195 |
+----------+--------------+------------+------+---------------+--------------+--------------------+
| 3 | Shipped | 180 | 32.4 | 18 | 0 | 194.4 |
+----------+--------------+------------+------+---------------+--------------+--------------------+
| 4 | cancelled | 600 | 108 | 50 | 108 | 550 |
+----------+--------------+------------+------+---------------+--------------+--------------------+
| 5 | shipped | 500 | 90 | 25 | 90 | 475 |
+----------+--------------+------------+------+---------------+--------------+--------------------+
| 6 | cancelled | 280 | 50.4 | 15 | 50.4 | 265 |
+----------+--------------+------------+------+---------------+--------------+--------------------+
please help.
Try this:
def new_col(x):
if x['Order Status'] == 'Shipped':
return x['item price'] + x.Tax - x['item discount'] - x['tax discount']
else:
return x['item price'] - x['item discount']
df['Total_Order_Amount'] = df.apply(new_col, axis=1)
Or without defining a function:
df['Total_Order_Amount'] = (
df['item_price']
+ df['Tax']
- df['item_Discount']
- df['Tax_Discount']
)
mask_shipped = df['Shipped'] != 'shipped'
df.loc[mask_shipped, 'Total_Order_Amount'] = (
df.loc[mask_shipped, 'item_price']
- df.loc[mask_shipped, 'item_Discount']
)
def func(a):
if a['Shipped'] == 'shipped':
return a['item_price'] + a['Tax'] - a['item_Discount'] - a['Tax_Discount']
elif a['Shipped'] == 'cancelled':
return a['item_price'] - a['item_Discount']
else:
return null
df['Total_Order_Amount'] = pd.Series(dtype='float64')
transformedDf = df['Total_Order_Amount'].apply(lambda x: func(x))

look up the average value based on 2 categories and calculate

enter image description here
I have data that will look up the average results of rows that fit the "X" category with the prefix of "id" with a data table like this
data
--------------------------
X | id | value
--------------------------
Mos | 1.1. | 74 | 78 | 70
Mos | 1.2. | 70 | 79 | 70
Mos | 2.1 | 70 | 76 | 73
Mos | 2.2 | 70 | 73 | 75
Mos | 3.1 | 77 | 72 | 78
Abs | 3.1 | 75 | 76 | 79
Abs | 3.2 | 74 | 80 | 77
Abs | 4.1 | 71 | 78 | 77
Abs | 4.2 | 76 | 76 | 76
Abs | 4.3 | 71 | 73 | 72
Cor | 1.2. | 75 | 70 | 78
Cor | 1.3. | 78 | 79 | 80
Cor | 2.1 | 79 | 70 | 70
Cor | 3.3 | 78 | 79 | 80
Cor | 4.1 | 78 | 74 | 72
Cor | 4.2 | 78 | 72 | 78
--------------------------
and the results as follows
result
------------------------
id | Mos | Abs | Cor
------------------------
1 | 73.5 | 0.0 | 76.7
2 | 72.8 | 0.0 | 73.0
3 | 75.7 | 76.8 | 79.0
4 | 0.0 | 74.4 | 75.3
how to make the formula?
Asume like this:
=IF("MOS" and LEFT(,1),SUM(D3:F4)/COUNT(D3:F4))
if you find "MOS" in the "X" column, according to the "id" that starts with (1 or 2 or 3 or 4), then the value table will add up and divide.
or
=AVERAGEIFS(D3:F18,B3:F18,I2,C3:F18,H3&"*")
Using a sample of your data, put this in H3 and copy down and across as necessary. An array formula so use Ctrl+Shift+Enter.
=AVERAGE(IF($A$2:$A$8=H$2,IF(VALUE(LEFT($B$2:$B$8,1))=$G3,$C$2:$E$8)))

Sumifs for rows and columns that are in an interval

I am trying to write a formula in excel that will take values from a table based on few criterias. Table looks like this:
+------------+----+----+----+-----+----+----+----+----+
| Month/Hour | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
+------------+----+----+----+-----+----+----+----+----+
| 1 | 93 | 21 | 32 | 42 | 55 | 30 | 98 | 97 |
| 2 | 41 | 73 | 25 | 58 | 48 | 10 | 19 | 20 |
| 3 | 87 | 17 | 92 | 100 | 9 | 31 | 22 | 22 |
| 4 | 92 | 91 | 63 | 28 | 95 | 58 | 55 | 47 |
| 5 | 56 | 34 | 69 | 60 | 87 | 64 | 40 | 53 |
| 6 | 8 | 64 | 92 | 30 | 48 | 27 | 52 | 65 |
| 7 | 77 | 6 | 27 | 45 | 29 | 91 | 24 | 90 |
| 8 | 13 | 14 | 31 | 10 | 40 | 49 | 22 | 57 |
| 9 | 70 | 16 | 55 | 95 | 85 | 41 | 65 | 17 |
| 10 | 72 | 79 | 81 | 48 | 13 | 40 | 99 | 52 |
| 11 | 76 | 88 | 80 | 45 | 97 | 68 | 64 | 62 |
| 12 | 73 | 82 | 97 | 74 | 93 | 7 | 6 | 71 |
+------------+----+----+----+-----+----+----+----+----+
In the first column we have months (1 - January, 2 - February, etc.) In the first row we have hours from 1 to 8.
Now I need a formula that for months <3,8> would sum values from hours <2,6> and for the rest of months (1-2, 9-12) would sum values from hours <4,7>. I've tried using Sumifs formula but it seems like it works only for one column at a time.
If it is not viable to use formula then I have some VBA knowledge and such solution would be welcome too.
Assuming months are in A2:A13 and the hours in B1:I1 you can use this formula
=SUMPRODUCT(ISNUMBER(MATCH(A2:A13,{3,8},0)*MATCH(B1:I1,{2,6},0))+0,B2:I13)
Vary as required for any combination of months/hours
list your months and hours then use MATCH inside an SUMPRODUCT:
=SUMPRODUCT(B2:I13,ISNUMBER(MATCH(B1:I1,M:M,0))*ISNUMBER(MATCH(A2:A13,L:L,0)))
Neither the Months nor the Hours desired needs to be consecutive.

excel I need formula in column name "FEBRUARY"

I have a set of data as below.
SHEET 1
+------+-------+
| JANUARY |
+------+-------+
+----+----------+------+-------+
| ID | NAME |COUNT | PRICE |
+----+----------+------+-------+
| 1 | ALFRED | 11 | 150 |
| 2 | ARIS | 22 | 120 |
| 3 | JOHN | 33 | 170 |
| 4 | CHRIS | 22 | 190 |
| 5 | JOE | 55 | 120 |
| 6 | ACE | 11 | 200 |
+----+----------+------+-------+
SHEET2
+----+----------+------+-------+
| ID | NAME |COUNT | PRICE |
+----+----------+------+-------+
| 1 | CHRIS | 13 | 123 |
| 2 | ACE | 26 | 165 |
| 3 | JOE | 39 | 178 |
| 4 | ALFRED | 21 | 198 |
| 5 | JOHN | 58 | 112 |
| 6 | ARIS | 11 | 200 |
+----+----------+------+-------+
The RESULT should look like this in sheet1 :
+------+-------++------+-------+
| JANUARY | FEBRUARY |
+------+-------++------+-------+
+----+----------+------+-------++-------+-------+
| ID | NAME |COUNT | PRICE || COUNT | PRICE |
+----+----------+------+-------++-------+-------+
| 1 | ALFRED | 11 | 150 || 21 | 198 |
| 2 | ARIS | 22 | 120 || 11 | 200 |
| 3 | JOHN | 33 | 170 || 58 | 112 |
| 4 | CHRIS | 22 | 190 || 13 | 123 |
| 5 | JOE | 55 | 120 || 39 | 178 |
| 6 | ACE | 11 | 200 || 26 | 165 |
+----+----------+------+-------++-------+-------+
I need formula in column name "FEBRUARY". this formula will find its match in sheet 2
Assuming the first Count value should go in cell E3 of Sheet1, the following formula would be the usual way of doing it:-
=INDEX(Sheet2!C:C,MATCH($B3,Sheet2!$B:$B,0))
Then the Price (in F3) would be given by
=INDEX(Sheet2!D:D,MATCH($B3,Sheet2!$B:$B,0))
I think this query will work fine for your requirement
SELECT `Sheet1$`.ID,`Sheet1$`.NAME, `Sheet1$`.COUNT AS 'Jan-COUNT',`Sheet1$`.PRICE AS 'Jan-PRICE', `Sheet2$`.COUNT AS 'Feb-COUNT',`Sheet2$`.PRICE AS 'Feb-PRICE'
FROM `C:\Users\Nagendra\Desktop\aaaaa.xlsx`.`Sheet1$` `Sheet1$`, `C:\Users\Nagendra\Desktop\aaaaa.xlsx`.`Sheet2$` `Sheet2$`
WHERE (`Sheet1$`.NAME=`Sheet2$`.NAME)
Provide Actual path insted of
C:\Users\Nagendra\Desktop\aaaaa.xlsx
First you need to know about how to make connection. So refer http://smallbusiness.chron.com/use-sql-statements-ms-excel-41193.html

Resources