I have been trying to get an excel formula work for over a day now. My Scenario is as below:
Department---------Sev1-----Sev2-------RAG
IT-------------------------0---------0---------Green
Transport --------------0---------2-----------Red
Admin-------------------0-------4 -------------Red
If Sev1 and Sev2 are both 0 then RAG should display text "green"
If Sev1 is more than 0 then RAG should display text "Red"
If Sev1 is 0 and Sev2 is 1 then RAG should display text "Amber"
If Sev1 is 0 and Sev2 is more than 1 then RAG should display text "Red"
Any help would me much appreciated!
Thanks
Joe
Assuming Sev1 is column B, Sev2 is column C
Also assuming Sev1, Sev2 are never negative, and Sev2 is never a fraction between 0 and 1.
if(B2>0, "Red", if(C2=0, "green", if(C2=1, "Amber", "Red")))
You can nest your if formulas. Assuming that your header starts in A1 then:
=IF(B2 > 0, "Red",IF(C2 = 1, "Amber", IF( C2 > 1, "Red", "green")))
Related
Background
I have the following toy df that contains lists in the columns Before and After as seen below
import pandas as pd
before = [list(['in', 'the', 'bright', 'blue', 'box']),
list(['because','they','go','really','fast']),
list(['to','ride','and','have','fun'])]
after = [list(['there', 'are', 'many', 'different']),
list(['i','like','a','lot','of', 'sports']),
list(['the','middle','east','has','many'])]
df= pd.DataFrame({'Before' : before,
'After' : after,
'P_ID': [1,2,3],
'Word' : ['crayons', 'cars', 'camels'],
'N_ID' : ['A1', 'A2', 'A3']
})
Output
After Before N_ID P_ID Word
0 [in, the, bright, blue, box] [there, are, many, different] A1 1 crayons
1 [because, they, go, really, fast] [i, like, a, lot, of, sports ] A2 2 cars
2 [to, ride, and, have, fun] [the, middle, east, has, many] A3 3 camels
Problem
Using the following block of code:
df.loc[:, ['After', 'Before']] = df[['After', 'Before']].apply(lambda x: x.str[0].str.replace(',', '')) taken from Removing commas and unlisting a dataframe produce the following output:
Close-to-what-I-want-but-not-quite- Output
After Before N_ID P_ID Word
0 in there A1 1 crayons
1 because i A2 2 cars
2 to the A3 3 camels
This output is close but not quite what I am looking for because After and Before columns have only one word outputs (e.g. there) when my desired output looks as such:
Desired Output
After Before N_ID P_ID Word
0 in the bright blue box there are many different A1 1 crayons
1 because they go really fast i like a lot of sports A2 2 cars
2 to ride and have fun the middle east has many A3 3 camels
Question
How do I get my Desired Output?
agg + join. The commas aren't present in your lists, they are just part of the __repr__ of the list.
str_cols = ['Before', 'After']
d = {k: ' '.join for k in str_cols}
df.agg(d).join(df.drop(str_cols, 1))
Before After P_ID Word N_ID
0 in the bright blue box there are many different 1 crayons A1
1 because they go really fast i like a lot of sports 2 cars A2
2 to ride and have fun the middle east has many 3 camels A3
If you'd prefer in place (faster):
df[str_cols] = df.agg(d)
applymap
In line
New copy of a dataframe with desired results
df.assign(**df[['After', 'Before']].applymap(' '.join))
Before After P_ID Word N_ID
0 in the bright blue box there are many different 1 crayons A1
1 because they go really fast i like a lot of sports 2 cars A2
2 to ride and have fun the middle east has many 3 camels A3
In place
Mutate existing df
df.update(df[['After', 'Before']].applymap(' '.join))
df
Before After P_ID Word N_ID
0 in the bright blue box there are many different 1 crayons A1
1 because they go really fast i like a lot of sports 2 cars A2
2 to ride and have fun the middle east has many 3 camels A3
stack and str.join
We can use this result in a similar "In line" and "In place" way as shown above.
df[['After', 'Before']].stack().str.join(' ').unstack()
After Before
0 there are many different in the bright blue box
1 i like a lot of sports because they go really fast
2 the middle east has many to ride and have fun
We can specify the lists we want to convert to string and then use .apply in a for loop:
lst_cols = ['Before', 'After']
for col in lst_cols:
df[col] = df[col].apply(' '.join)
Before After P_ID Word N_ID
0 in the bright blue box there are many different 1 crayons A1
1 because they go really fast i like a lot of sports 2 cars A2
2 to ride and have fun the middle east has many 3 camels A3
This was a fairly difficult question to put to words but hopefully the example data and my attempt will help
I have the following
Apple 17 1 0 0 0 0 0 0 0 0 0 0
Orange 14 1 15 1 6.67 1 6.67 1 6.67 2 13.33 10
Banana 15 3 5 0 0 0 0 0 0 0 0 1
Cherry 13 1 12 2 16.67 2 16.67 2 16.67 2 16.67 2
Peach 16 4 12 1 8.33 1 8.33 2 16.67 2 16.67 8
Strawberry 12 5 6 1 16.67 1 16.67 1 16.67 1 16.67 7
I am trying to find the max value in M and then display A from the same row. So in this example the max value would be 10 and "Orange" should be displayed.
It should be noted that I am using 2 sheets, "Data" has my output and "Raw Data" has... the raw data
=VLOOKUP(MAX('Raw Data'!M1:M6), A1:M6, 1, FALSE)
This produces #N/A and I think it might have something to do with my data either not being formatted into a table (though doing so seems to fail with a different error) or running from one sheet to the other.
Any input would be greatly appreciated
VLOOKUP requires that the value to find is in the first column.
Use INDEX/MATCH:
=INDEX(A1:A6,MATCH(MAX(M1:M6),M1:M6,0))
Try this =INDIRECT(ADDRESS(MATCH(MAX(M1:M6),M1:M6,0),1))
I recomend using Index Match as Scott Craner has shown.
But if you are set on using vlookup:
I will first shortly explain vlookup as how I have understood it.
First a limitation, vlookup can only return values to the left of your search range, i.e you cant use negative numbers i.e,
=vlookup(Max('Raw Data'!M1:M6), A1:M6, **"-1"**, False)
Secondly, vlookup is structured like this:
=vlookup(The value you want to search for, the range you want to work with, which column you want to return a value from (1 means the first column).
vlookup will allways search for the vale you want to search for in the first column.
So in your data:
You are telling vlookup to search for the maximum value that exists in the range M1:M6. In this case 10 right?
You are telling vlookup to search for this value in column A which contains the fruits. This is where it goes wrong.
My solution to this would be to move all the fruits names to the N column (to the left of you values you want to search).
In code:
In the N Column:
Put = A and the row number
Now do a vlookup like this:
=vlookup(Max('Raw Data'!M1:M6), M1:N6, 2, False)
I hope this can solve your problem.
Best regards,
I have two columns
Quantity Date
2 02/01/2014
3 04/01/2014
4 07/01/2014
5 07/01/2014
1 08/01/2014
3 08/01/2014
I want to add the quantity values for each date and put 0 for dates those are missing. So end result would look like below
Quantity Date
0 01/01/2014
2 02/01/2014
0 03/01/2014
3 04/01/2014
0 05/01/2014
0 06/01/2014
9 07/01/2014
4 08/01/2014
0 09/01/2014
. ...
. ...
. ...
Can't think of a way, can someone help please
I don't have Excel in front of me, but I'll take a stab at it.
Leave your first dataset in Sheet1, but reverse the columns.
Then type
Date
01/01/2014
02/01/2014
03/01/2014
....
...into Column A of Sheet2 and drag the cells down to expand the series as far as it needs to go.
Then do a =vlookup(A1, Sheet1!A$1$:B$99999$, 2, FALSE) in column B of sheet two.
You'll get errors, where you don't have dates in Sheet1, so you can fill those in by modifying the above equation to: =IFERROR(vlookup(A1, Sheet1!A$1$:B$99999$, 2, FALSE), 0)
(I think).
How do you return different values in a cell based on which range the value entered in another cell comes under? Specifically, I am trying to make a step function.
For example:
IF G2 is ABOVE "0" BUT BELOW "1" THEN display "0.1"
IF G2 is ABOVE "0.99" BUT BELOW "5" THEN display "0.15"
IF G2 is ABOVE "4.99" BUT BELOW "15" THEN display "0.2"
IF G2 is ABOVE "14.99" BUT BELOW "30" THEN display "0.5"
IF G2 is ABOVE "29.99" BUT BELOW "100" THEN display "1.0"
IF G2 is ABOVE "99.99" THEN display "1.30"
So IF G2 was "£18.75" then the cell that this formula is entered in would display "£0.50" based on the value's above.
(bear in mind that this is specific to my spreadsheet and was for calculating prices i.e. 0.99 = £0.99)
Following #oli_taz's suggestion, here is a slightly more robust solution that can deal with any input:
=IF(D4<F4, 0, VLOOKUP(D4,F4:G9,2))
with the range F4:G9:
0 0.1
1 0.15
5 0.2
15 0.5
30 1
100 1.3
and D4 being the value in question, e.g. 18.75 -> result: 0.5
Numbers smaller than 0 will return 0 and numbers larger than 100 will return 1.3.
Nested if's in Excel Are ugly:
=If(G2 < 1, .1, IF(G2 < 5,.15,if(G2 < 15,.2,if(G2 < 30,.5,if(G2 < 100,.1,1.3)))))
That should cover it.
Heres the issue I have a dump from the database at work that is in a rather conveluted format. Basically it does not give you the displayed information as much as just the individual relation tables to work with.
EXAMPLE: lets say I have the following columns of information
ID, COLOR, SIZE, QTY, TYPE
the information looks something like this
ID COLOR SIZE QTY TYPE
A brown 20 1 1
C yellow 10 2 2
D brown 40 5 1
A blue 70 1 3
A yellow 80 1 2
B yellow 20 4 1
D blue 70 4 2
C blue 10 3 1
what i need is something more like this
ID BROWN SIZE TYPE BLUE SIZE TYPE YELLOW SIZE TYPE
A 1 20 1 1 0 3 1 80 2
B 0 0 0 0 0 0 4 20 1
C 0 0 0 3 10 1 2 20 1
D 5 40 1 4 0 2 0 0 0
I most like could accomplish this with an excel formula, possibly the one called sumifs but i can not seem to get it to work any help with this would be greatly appriciated.
You can do this by using a combination of SUM and IF in array formulas.
Assuming that your data table starts in cell A1 and your result table starts in cell A11, begin by entering the following formulas in cells B12, C12 and D12 respectively, making sure to use the CONTROL-SHIFT-ENTER key combination to enter them.
B12 =SUM(IF($B$2:$B$9=B$11,IF($A$2:$A$9=$A12,$D$2:$D$9,0)))
C12 =SUM(IF($B$2:$B$9=B$11,IF($A$2:$A$9=$A12,$C$2:$C$9,0)))
D12 =SUM(IF($B$2:$B$9=B$11,IF($A$2:$A$9=$A12,$E$2:$E$9,0)))
Copy the cells down to the bottom of your data table, which in this example would be row 15.
Then copy the block of formulas you have created to cell E12, where the BLUE section of the result table starts. Then copy the same block of formulas to cell H12, where the YELLOW section of the result table starts.
This solution assumes that you have no duplicate combinations of ID and COLOR in your data table and that no combinations are missing. If there are missing combinations of ID and COLOR, you would need to wrap the formulas in an IFERROR function (in Excel 2010).
The way I'd transpose a data table would run like this...
Your example is a bit more complicated so I'm not going to use your full data, just go with size. Begin by creating a column called Index
A B C D
INDEX ID COLOR SIZE
=A3 & "-" & B3 A brown 20
=A4 & "-" & B4 C yellow 10
. D brown 40
. A blue 70
. A yellow 80
. B yellow 20
D-blue D blue 70
C-blue C blue 10
Create a table equivalent which combines the header row with the ID column
F G H I
ID brown blue yellow
A =$F2 & "-" & G$1 . .
B . . .
C . C-blue C-yellow
D . D-blue D-yellow
Finally wrap this up in a VLOOKUP or an OFFSET(MATCH(_)) with possible an IFERROR
ID brown blue yellow
A =IFERROR(VLOOKUP($F2 & "-" & G$1,$A$3:$D$10,4,FALSE),"Err")
B . . .
C . 10 10
D . 70 Err
Anyway that is how I generally do this sort of transform