Comparing boolean cells in excel? - excel

i have some problem in how to compare boolean cells.
Let say like this
A B C D E F G H I
1 1 1 1 1 0
1 0 1 1 1 1
0 1 1 0 1 1
0 0 0 1 1 1
In cell G2 i need to calculate and compare every 3 celss.
if "sum" of A2:C2 = 3, then return 0, but if "sum" of A2:C2 <=2, then "sum" B2:D2, if "sum" of B2:D2 = 3 then return 1 else 0
In cell H2 i need to do the same.
if "sum" of B2:D2 = 3, then return 0, but if "sum" of B2:D2 <=2, then "sum" C2:E2, if "sum" of C2:E2 = 3 then return 1 else 0
and so on
i already try IF, AND, SUM but still not working.
Thanks for your help...

Related

How to return all rows that have equal number of values of 0 and 1?

I have dataframe that has 50 columns each column have either 0 or 1. How do I return all rows that have an equal (tie) in the number of 0 and 1 (25 "0" and 25 "1").
An example on a 4 columns:
A B C D
1 1 0 0
1 1 1 0
1 0 1 0
0 0 0 0
based on the above example it should return the first and the third row.
A B C D
1 1 0 0
1 0 1 0
Because you have four columns, we assume you must have atleast two sets of 1 in a row. So, please try
df[df.mean(1).eq(0.5)]

How count cells without range and some criterion using excell

I hope, I can help me!
I have a data frame as this:
A B C D E F G
1 4 2 3 0 0 0 (Here the formula)
2 0 0 0 0 0 0 (Here the formula)
3 3 2 4 5 0 0 (Here the formula)
4 0 0 0 0 5 2 (Here the formula)
5 0 0 0 5 5 5 (Here the formula)
in column G, a counting formula must be according to the following criteria: With <>:distinct of
I have sum the count(if(A2<>0 and A3<>0) + (B2 and B3)....
for example: I tried to make this;
in G1
=SUM(COUNT(IF(AND(A1<>0;A2<>0);1;0);COUNT(IF(AND(B1<>0;B2<>0);1;0);COUNT(IF(AND(C1<>0;C2<>0);1;0).....COUNT(IF(AND(F1<>0;F2<>0);1;0);COUNT(IF(AND(A1<>0;A3<>0);1;0);COUNT(IF(AND(B1<>0;B3<>0);1;0);COUNT(IF(AND(C1<>0;C3<>0);1;0).....COUNT(IF(AND(F1<>0;F3<>0);1;0);............
COUNT(IF(AND(A1<>0;A5<>0);1;0);COUNT(IF(AND(B1<>0;B5<>0);1;0);COUNT(IF(AND(C1<>0;C5<>0);1;0).....COUNT(IF(AND(F1<>0;F5<>0);1;0))
for each G2, G3,...G5
that is to say, I want to count only the cells in which there are only non-zero values in the same column and add them, it would be something like this:
G1 is equal to:
       
= (0 + 3 + 0 + 0)
G2 is equal to:
       
= (0 + 0 + 0 + 0)
G3 is equal to:
       
= (3 + 0 + 0 + 1)
G4 is equal to:
       
= (0 + 0 + 0 + 0)
G4 is equal to:
       
= (0 + 0 + 1 + 2)
I hope you can help me
Put this in G1:
=SUMPRODUCT((A1:F1<>0)*($A$1:$F$5<>0)*(ROW($A$1:$F$5)<>ROW(A1:F1)))
And copy down.

Populate cells based on x by y cell value

I'm trying to populate cells based on values from two different cells.
Values in the cell needs to be (n-1) where n is the input and then repeated based on the amount of the other cell.
For example, I have input:
x y
2 5
Output should be:
x should have 0 and 1; each repeated five times
y should have 0, 1, 2, 3, 4; each repeated twice
x1 y1
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
I used:
=IF(ROW()<=C2+1,K2-1,"")
and
=IF(ROW()<=d2+1,K2-1,"")
but it is not repeating and I only see:
x y
0 0
1 1
__ 2
__ 3
__ 4
(C2 and D2 are where values for x and y are, K is the number of items.)
Are there any suggestions on how I can do this?
In Row2 and copied down to suit:
=IF(ROW()<=1+C$2*D$2,INT((ROW()-2)/D$2),"")
and
=IF(ROW()<=1+C$2*D$2,MOD(ROW()-2,D$2),"")

Excel check for value in different cells per row

Having this kind of data:
A B C D E
1 1 0 1 0 0
2 0 1 1 0 1
3 1 0 1 1 0
4 0 1 0 1 0
I would like to show true/false in column F if column A, C and E has the value of 1.
So not looking for a value in range - but different columns.
You can use the AND function, something like:
=IF(AND(A1=1,C1=1,E1=1),"TRUE","FALSE")
=IF(A2;IF(C2;IF(E2;TRUE;FALSE);FALSE);FALSE)
This will display TRUE if ALL three cells are 1, else FALSE.

Index Value of Last Matching Row Python Panda DataFrame

I have a dataframe which has a value of either 0 or 1 in a "column 2", and either a 0 or 1 in "column 1", I would somehow like to find and append as a column the index value for the last row where Column1 = 1 but only for rows where column 2 = 1. This might be easier to see than read:
d = {'C1' : pd.Series([1, 0, 1,0,0], index=[1,2,3,4,5]),'C2' : pd.Series([0, 0,0,1,1], index=[1,2,3,4,5])}
df = pd.DataFrame(d)
print(df)
C1 C2
1 1 0
2 0 0
3 1 0
4 0 1
5 0 1
#I've left out my attempts as they don't even get close
df['C3'] = IF C2 = 1: Call Function that gives Index Value of last place where C1 = 1 Else 0 End
This would result in this result set:
C1 C2 C3
1 1 0 0
2 0 0 0
3 1 0 0
4 0 1 3
5 0 1 3
I was trying to get a function to do this as there are roughly 2million rows in my data set but only ~10k where C2 =1.
Thank you in advance for any help, I really appreciate it - I only started
programming with python a few weeks ago.
It is not so straight forward, you have to do a few loops to get this result. The key here is the fillna method which can do forwards and backwards filling.
It is often the case that pandas methods does more than one thing, this makes it very hard to figure out what methods to use for what.
So let me talk you through this code.
First we need to set C3 to nan, otherwise we cannot use fillna later.
Then we set C3 to be the index but only where C1 == 1 (the mask does this)
After this we can use fillna with method='ffill' to propagate the last observation forwards.
Then we have to mask away all the values where C2 == 0, same way we set the index earlier, with a mask.
df['C3'] = pd.np.nan
mask = df['C1'] == 1
df['C3'].loc[mask] = df.index[mask].copy()
df['C3'] = df['C3'].fillna(method='ffill')
mask = df['C2'] == 0
df['C3'].loc[mask] = 0
df
C1 C2 C3
1 1 0 0
2 0 0 0
3 1 0 0
4 0 1 3
5 0 1 3
EDIT:
Added a .copy() to the index, otherwise we overwrite it and the index gets all full of zeroes.

Resources