Retrieving Max in range of one column dictated by another column - excel

My set up is fairly simple. I have paired data where one column is time and the next is a value corresponding to that time point. This recurs for many trials with each trial having a different number of time points
Time Freq
0.216 0.000
0.423 4.835
0.620 5.067
0.784 6.108
0.971 5.355
1.156 5.395
1.311 6.470
1.433 8.170
1.575 7.034
1.752 5.673
1.925 5.758
2.077 6.602
2.180 9.675
2.363 5.477
2.487 8.022
2.616 7.795
2.773 6.344
2.915 7.050
3.074 6.283
3.208 7.495
3.395 5.344
3.535 7.111
3.682 6.839
3.830 6.730
4.023 5.185
This is an example from a table. What I want to do is to create a formulate that will pull the Max Frequency when Time is greater that 1 and less than 3. I know this can be done by manually selecting the range, but I have many different ranges that I want to find the max freq for would like to be able to just input the column.

You can reference upper and lower bounds for the time variable like this:
+---+----+----+-------+
| | D | E | F |
+---+----+----+-------+
| 1 | LB | UB |MaxFreq|
| 2 | 1 | 3 | 9.675 |
| 3 | 0 | 1 | 6.108 |
| 4 | 1 | 2 | 8.17 |
| 5 | 2 | 3 | 9.675 |
+---+----+----+-------+
F2: =MAX(IF(($A$1:$A$26>$D2)*($A$1:$A$26<$E2),$B$1:$B$26))
F2 is an array formula--confirm the entry with the combination Ctrl+Shift+Enter (not just Enter). It can be copied down as far as needed.

Related

How do I search for a string in a pandas column and append to the row based on that string?

I have a pandas dataframe and I want to search through strings in column A, if there's a match I want to append 1 to a new column, if there is no match I want to append a 0.
My df currently looks like:
Column A | Column B | Column C
company one | 314 | 0.9
company one toast | 190 | 0.3
www.companyone | 380 | 0.87
companyone home | 850 | 0.1
toaster supplies | 1100 | 0.5
toast rack | 200 | 0.7
...
I'm trying to write a function which will read through column A, and if there's a match with either company one or companyone, then append 1 on the end of the row. If there is no match, then append 0. The output I'm looking for is:
Column A | Column B | Column C | Branded
company one | 314 | 0.9 | 1
company one toast | 190 | 0.3 | 1
www.companyone | 380 | 0.87 | 1
companyone home | 850 | 0.1 | 1
toaster supplies | 1100 | 0.5 | 0
toast rack | 200 | 0.7 | 0
...
I've tried this function:
def branded(table):
if 'company.*?one' in table[table['Column A']]:
table['Branded'] = 1
else:
table['Branded'] = 0
return table.head()
However I get a KeyError. I'm not sure what I'm missing though.
You can do it like this:
df['Branded'] = df['Column A'].str.contains('company.*?one')*1
The solution posted by zipa is better in my opinion. However, thought of sharing this which is a tweak version in case the strings to be looked for are entirely of different pattern. You can add the words to the list and then perform something similar:
import pandas as pd
df = pd.DataFrame({'column':['company one','companyone', 'company two']})
search = ['company one', 'companyone']
string_search = '|'.join(search)
df['flag'] = df['column'].str.contains(string_search)
df['flag'] = df['flag'].map({True: 1, False: 0})

Google Sheets Divide value if other column not empty

I have a sheet with the following values in minutes to keep track of how long it takes for x task:
+---------+----------+----------+
| A | B | C |
+---------+----------+----------+
| Task | Person 1 | Person 2 |
| Task #1 | 10 | 20 |
| Task #2 | 20 | 0 |
| Task #3 | 0 | 30 |
+---------+----------+----------+
I want to get total of hours for every task but if a task has time for Person 1 and Person 2, I want the average of the time taken for the task.
What I've had in mind previously is:
=(SUM(Tasks!$B$2:$B) + SUM(Tasks!$C$2:$C)) / 2
I thought this would work but then I realized it won't because some tasks are only handled by Person 1.
The total of the previous formula would give me 40, but what I'm expecting as a value should be 65, based on the following calculation:
(10 + 20) / 2 + 20 + 30
=SUMPRODUCT((B2:B4+C2:C4)/((B2:B4>0)+(C2:C4>0)))
Use this array formula:
=SUM(IF((B2:B4>0)*(C2:C4>0),(B2:B4+C2:C4)/2,B2:B4+C2:C4))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.

Scaling values with a known upper limit

I have a column of values in Excel that I need to modify by a scale factor. Original column example:
| Value |
|:-----:|
| 75 |
| 25 |
| 25 |
| 50 |
| 0 |
| 0 |
| 100 |
Scale factor: 1.5
| Value |
|:-----:|
| 112.5 |
| 37.5 |
| 37.5 |
| 75 |
| 0 |
| 0 |
| 150 |
The problem is I need them to be within a range of 0-100. My first thought was take them as percentages of 100, but then quickly realized that this would be going in circles.
Is there some sort of mathematical method or Excel formula I could use to handle this so that I actually make meaningful changes to the values, such that when these numbers are modified, 150 is 100 but 37.5 might not be 25 and I'm not just canceling out my scale factor?
Assuming your data begin in cell A1, you can use this formula:
=MIN(100,A1*1.5)
Copy downward as needed.
You could do something like:
ScaledValue = (v - MIN(AllValues)) / (MAX(AllValues) - MIN(AllValues)) * (SCALE_MAX - SCALE_MIN) + SCALE_MIN
Say your raw data (a.k.a. AllValues) ranges from a MIN of 15 to a MAX of 83, and you want to scale it to a range of 0 to 100. To do that you would set SCALE_MIN = 0 and SCALE_MAX = 100. In the above equation, v is any single value in the data.
Hope that helps
Another option is:
ScaledValue = PERCENTRANK.INC(AllValues, v)
In contrast to my earlier suggestion, (linear --- preserves relative spacing of the data points), this preserves the order of the data but not spacing. Using PERCENTRANK.INC will have the effect that sparse data will get compressed closer together, and bunched data will get spread out.
You could also do a weighted combination of the two methods --- give the linear method a weight of say 0.5 so that relative spacing is partially preserved.

Increment a number inside excel formula sidewise

I have following numbers in Column D1 to D21
1 | 4 | 51 | 4 | 57 | 6 | 16 | 11 | 41 | 3 | 26 | 3 | 27 | 5 | 3 | 5 | 8 | 6 | 22 | 6 | 23
I want to write a formula which will produce an output like this:
6:23 6:22 5:8 5:3 3:27 3:26 11:41 6:16 4:47 4:51 1
I have put the following formula on Column P1 and tried dragging on the left side but it is showing the same value in all rows as the value of 1 & 2 is not changing to 3 & 4:
This is what i want:
OFFSET($D$1,1,0)&":"&OFFSET($D$1,2,0)
OFFSET($D$1,3,0)&":"&OFFSET($D$1,4,0)
OFFSET($D$1,5,0)&":"&OFFSET($D$1,6,0)
The middle value in the above formula should change.
I've tested with a smaller range of values and the following should work for you. You'll need to replace $A$1:$I$1 with your range.
=IFERROR(OFFSET($A$1,0,COUNT($A$1:$I$1)-(COLUMN()*2))&":"&OFFSET($A$1,0,COUNT($A$1:$I$1)-((COLUMN()*2)-1)),IF((COUNT($A$1:$I$1)+1)=(COLUMN()*2),$A$1,""))
Also tested with your full range $A$1:$U$1:
=IFERROR(OFFSET($A$1,0,COUNT($A$1:$U$1)-(COLUMN()*2))&":"&OFFSET($A$1,0,COUNT($A$1:$U$1)-((COLUMN()*2)-1)),IF((COUNT($A$1:$U$1)+1)=(COLUMN()*2),$A$1,""))

How to get a range of values from an openoffice calc formula?

Am encountered an issue on openOffice Calc.
I need to get a range of values from a formula, that can be used in a dropdownlist/validity.
I have a sheet with following data.
A B C
+---------------
1 | 10 x
2 | 20 x
3 | 30 y
4 | 40 z
5 | 50 x
6 |---------------
Here I need a list of values of 'A' where values 'B' equals 'x'.
I have checked with LOOKUP/INDEX functions, but it returns a single value(first occurrence) not a range.
Try this formula:
=IFERROR(INDEX($A$1:$A$5, SMALL(IF($B$1:$B$5="x",ROW($B$1:$B$5),9^99),ROW())),"")
^-------^ ^-------^ ^ ^-------^ ^--^
| | | | +-> Random big number
| | | +-> Range to check
| | +-> Value to check
| +-> Range to check
+-> Range to return
You'll need to use Ctrl+Shift+Enter to make it work, then drag the formula down.
If you start on row 2, you'll have to use ROW()-1 for it to work. It's generally ROW()-(k-1) where k is the row number you're using the formula first.

Resources