I need a count of the number of rows that have a value in each of two columns - excel

Lets say I have two columns B2:B21 and T2:T21
I need the total number of rows that have a value in both column B and T
If B2 has a value of 15, and T2 is blank, don't count that row
If B2 is blank and T2 has a value of 5, don't count that row
If B2 is blank and T2 is blank, don't count that row
If B2 is 45 and T2 is 50, then count this row (+1)
id this clear?

Next time try Super Users when you do not need programming assistance.
=COUNTIFS(B2:B21,">0",T2:T21,">0")

If you want to count a row if for example, B2 has 0 and T1 has 15 (+1), then use this instead:
=COUNTIFS(B:B,"<>"&"",T:T,"<>"&"")
This also works for cells containing text.

Related

Pandas group by two columns and get top n rows of each value of one of the columns sorted in descending order

I have a pandas dataframe with many columns (Two column names of interest are a and b)
I want to group by a and b
compute the occurences of each group
sort each group in descending order of occurrences
For each value of b I want to take top n values of a, which have most occurences.
I could do upto step 3, using the following code:
a_b_count = df.groupby(['a', 'b']).size().reset_index().rename({0:'count'},axis='columns').sort_values('count', ascending = False)
But, for each value of b, how to get top-n values of a for which occurrences are the highest?
Example
df =
a b ...
a1 b1 ...
a2 b1 ...
a1 b1 ...
a1 b2 ...
a2 b2 ...
a2 b2 ...
Expected Output (for n = 1):
a b count
b1 a1 2
b2 a2 2
You can use nlargest rather than a sort. Will be faster for a smaller n relative to Series size.
df.groupby(['a', 'b']).size().groupby(
level=1).nlargest(n).reset_index(-1, drop=True)
b a
b1 a1 2
b2 a2 2
dtype: int64
Here's one way to do it, using crosstab to get a frequency of columns a and b :
pd.crosstab(df.a, df.b).stack().nlargest(1, keep="all").reset_index(name="count")

Formula to add a number to the cell and change that value if it is greater than certain number

In Column A I have numbers from 1 to 30 and in Column B I want to add 10 to the number in column A i.e.
if A1 is 1 I want 11 in B1 and in C1 I want 21 and
if A2 is 2 I want 12 in B2 and in C2 I want 22.
I don't want the numbers in columns B and C to exceed the maximum limit of numbers I have in Column A, in this case that is 30.
I need a formula to achieve this so that none of the cells in column B and C has the number exceeding 30 and if the addition exceeds that I want 30 to be subtracted from it. I tried this with IF but it didn't help as I don't know how to manipulate the current cell after adding the number.
Screenshot of expected results:
I want the number in B Column to be addition of 10 to the number in A Column and it should not exceed maximum number.
Maybe, in B1:
=A1+10-30*(A1+10>30)
and in C1, both copied down to suit:
=B1+10-30*(B1+10>30)
Considering B1, the above can be simplified slightly by rendering (A1+10>30) down to (A1>20).
A1+10 is the basic requirement.
The condition is that where A1+10 exceeds 30, a deduction of 30 should be applied. This can be restated as "where A1 exceeds 20".
(A1>20) returns either TRUE (when A1 is more than 20) and FALSE (otherwise).
What may not have been appreciated by all is that when used as a multiplier, Excel treats TRUE as 1 and FALSE as 0.
So when A1 is less than or equal to 20, nothing is deducted from to A1+10. 30*(A1>20) becomes 30*FALSE (effectively 30*0), which is 0. However, when A1 is more than 20 30*(A1>20) becomes 30*TRUE (effectively 30*1), which is 30.
The formula for B1 is:
=IF(A1+10>MAX($A$1:$A$30),A1+10-MAX($A$1:$A$30),A1+10)
(For C1 just do "Fill Right" from B1.)
Sometimes it's better to use several columns to get to the result:
First I created in D1 =A1+10, then in E1 =MAX($A$1:$A$30).
The $ means that the column or row number does not change when you fill down, so A1 stays fixed to A1 in every cell.
In F1 I created an IF formula: =IF(D1>$E$1,D1-$E$1,D1) which means "IF D1 exceeds the highest value in A1 to A30 THEN subtract that highest value from D1 ELSE just display D1 (=A1+10)"
And if you substitute these values:
(E1): =IF(D1>MAX($A$1:$A$30),D1-MAX($A$1:$A$30),D1)
(D1): =IF(A1+10>MAX($A$1:$A$30),A1+10-MAX($A$1:$A$30),A1+10)
you get the result I mentioned in the first place.

Dragging formulas across - Increment columns by more than 1

I can't seem to find anything similar that's already been asked (they all relate to incrementing row numbers rather than columns)
I'm looking to drag a formula across horizontally and have the columns increment by 2
E.g. B1-A1, D1-C1, F1-E1...
Thanks!
You'll need to have a value in cell A1 and B1 for the following to work.
For my testing I put the number 1 in A1 and B1.
Try this in Cell C1:
=IF(MOD(COUNT($A$1:B1),2)=0,COLUMN(B1),IF(B1<>A1,B1,A1))
Here's what you should see when you drag that formula across:
A B C D E F G H I J K L M N
1 1 2 2 4 4 6 6 8 8 10 10 12 12
And this is what the formula does:
The MOD(COUNT() part of the formula counts the cells to the left of it, and if they are a multiple of 2, the value changes.
I've left the value to change to (the 'new' value) as the COLUMN() number for the cell before, just for example's sake. but you can change this part.
The last IF statement at the end checks if the cell before is equal to the cell before that, (eg. Is CELL C1 equal to CELL B1) and if they are not equal, it will give the cell before as a value (the 'copy' value).

Dividing a column into N equal groups by value

Say I have a column with values:
23
24
25
66
67
84
81
85
I want to divide this into N groups, say N right now is 4.
23,1
24,1
25,2
66,2
67,3
84,3
81,4
85,4
I actually need to divide around 30k sorted values into groups 1 to 99; each with equal number of elements.
Any quick way to do this in Excel?
With data in column A, in B1 enter:
=A1 & "," & ROUNDUP(ROW()/(COUNT(A:A)/4),0)
and copy down. For example:
.
Change the 4 in the formula to vary the number of groups.
I use this trick for equal data bucketing. Suppose you have data in A1:A8 range. Put this formula in B1:
=MAX( ROUNDUP( PERCENTRANK($A$1:$A$8, A1) *4, 0),1)
Fill down the formula all across B column and you are done. The formula divides the range into 4 equal buckets and it returns the bucket number which the cell A1 falls into. The first bucket contains the lowest 25% of values.
Adjust the number of buckets according to thy wish:
=MAX(ROUNDUP(PERCENTRANK([Range],[OneCellOfTheRange]) *[NumberOfBuckets],0),1)
The number of observation in each bucket will be equal or almost equal. For example if you have a 100 observations and you want to split it into 3 buckets then the buckets will contain 33, 33, 34 observations. So almost equal. You do not have to worry about that - the formula works that out for you.
if this is in column A
row 1
row 2
row 3
row 4
row 5
place formula in column B
=MOD(ROW(); 4)+1
this result in
row 1, 2
row 2, 3
row 3, 4
row 4, 1
row 2, 2

Change value in a cell based on value in another cell

Searched for this but could not find a way to do it.
I would like to be able to transform a value in one cell to another value in a different cell like this:
When cells in Column A contain Y set same number cells in Column B to Male or when cells in Column A contains N set same number cells in Column B value to Female.
For instance:
A2 = Y then B2 = Male
A2 = N then B2 = Female
=IF(A2="Y","Male",IF(A2="N","Female",""))
by typing yes it wont charge taxes, by typing no it will charge taxes.
=IF(C39="Yes","0",IF(C39="no",PRODUCT(G36*0.0825)))
If you want to do something like the following example, you'd have to use nested ifs.
If percentage is greater than or equal to 93%, then corresponding value in B should be 4 and if the percentage is greater than or equal to 90% and less than 92%, then corresponding value in B to be 3.7, etc.
Here's how you'd do it:
=IF(A2>=93%, 4, IF(A2>=90%, 3.7,IF(A2>=87%,3.3,0)))

Resources