Complete Truth Tables Based On Binary - io

I am trying to figure out names for every combination with truth tables.
In the first table, I have each truth table for a two input and one output system. The inputs are read by row. The outputs are in a binary counted format. Each Output is read by column and is labeled with a hex number 0 to F. The input by row is related to the outputs within the specified output column.
In the second table, I have listed by row how each output column on the first chart works. In each row I have listed the binary logic gate name, if statement in javascript, and a description for how each would work. I have a hyphen for spaces that are not complete.
Are there names for the blank spaces in the gate names in the second table?
Complete Truth Tables
Inputs | Outputs
1 2 | 0 1 2 3 4 5 6 7 8 9 A B C D E F
-----------------------------------------
0 0 | 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 1 | 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
1 0 | 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
1 1 | 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
Num | Gate | Javascript | Return True If
--- | ----- | ---------- | --------------
0 | - | 0 | FALSE
1 | AND | I1&&I2 | I1 AND I2
2 | - | I1&&!I2 | I1 AND NOT I2
3 | - | I1 | I1
4 | - | !I1&&I2 | I2 AND NOT I1
5 | - | I2 | I2
6 | XOR | I1!==I2 | I1 NOT EQUALS I2
7 | OR | I1||I2 | I1 OR I2
8 | NOR | !I1||!I2 | NOT I1 OR NOT I2
9 | XNOR | I1==I2 | I1 EQUALS I2
A | - | !I2 | NOT I2
B | - | !(!I1&&I2) | NOT ( I2 AND NOT I1 )
C | - | !I1 | NOT I1
D | - | !(I1&&!I2) | NOT ( I1 AND NOT I2 )
E | NAND | !I1&&!I2 | NOT I1 AND NOT I2
F | - | 1 | TRUE

Some of the other combinations have gate names, but not all do.
The A and C cases are each an example of a NOT gate, and the 3 and 5 cases are each an example of a BUFFER.
The D case is known as an IMPLY gate, but this is not as commonly known as the others.
For the rest, there are no commonly used gate names because to implement their boolean function would require either no gates (as in TRUE and FALSE), or they would require a combination of two or more of the conventional gates that you have already identified. There may be specific implementations of tools or systems that have created names for these "quasi-gates", but they are not in common use.
See Also
Logic Gate (Wikipedia)
Imply Gate (Wikipedia)

Related

How to efficiently disaggregate data from?

I have Google Analytics data which I am trying to disaggregate.
Below is a simplified version of the dataframe I am dealing with:
date | users | goal_completions
20150101| 2 | 1
20150102| 3 | 2
I would like to disaggregate the data such that each "user" has its own row. In addition, the third column, "goal_completions" will also be disaggregated with the assumption that each user can only have 1 "goal_completion".
The output I am seeking will be something like this:
date | users | goal_completions
20150101| 1 | 1
20150101| 1 | 0
20150102| 1 | 1
20150102| 1 | 1
20150102| 1 | 0
I was able to duplicate each row based on the number of users on a given date, however I can't seem to find a way to disaggregate the "goal_completion" column. Here is what I currently have after duplicating the "users" column:
date | users | goal_completions
20150101| 1 | 1
20150101| 1 | 1
20150102| 1 | 2
20150102| 1 | 2
20150102| 1 | 2
Any help will be appreciated - thanks!
IIUC using repeat create you dfs , then we adjust the two column by cumcount with np.where
df=df.reindex(df.index.repeat(df.users))
df=df.assign(users=1)
df.goal_completions=np.where(df.groupby(level=0).cumcount()<df.goal_completions,1,0)
df
Out[609]:
date users goal_completions
0 20150101 1 1
0 20150101 1 0
1 20150102 1 1
1 20150102 1 1
1 20150102 1 0

Creating A new column based on other columns' values with specific requirement in Python Dataframe

I want to create a new column in Python dataframe with specific requirements from other columns. For example, my python dataframe df:
A | B
-----------
5 | 0
5 | 1
15 | 1
10 | 1
10 | 1
20 | 2
15 | 2
10 | 2
5 | 3
15 | 3
10 | 4
20 | 0
I want to create new column C, with below requirements:
When the value of B = 0, then C = 0
The same value in B will have the same value in C. The same values in B will be classified as start, middle, and end. So for values 1, it has 1 start, 2 middle, and 1 end, for values 3, it has 1 start, 0 middle, and 1 end. And the calculation for each section:
I specify a threshold = 10.
Let's look at values B = 1 :
Start :
C.loc[2] = min(threshold, A.loc[1]) + A.loc[2]
Middle :
C.loc[3] = A.loc[3]
C.loc[4] = A.loc[4]
End:
C.loc[5] = min(Threshold, A.loc[6])
However, the output value of C will be the sum of the above calculations.
When the value of B is unique and not 0. For example when B = 4
C[10] = min(threshold, A.loc[9]) + min(threshold, A.loc[11])
I can solve point 0 and 3. But I'm struggling to solve point 2.
So, the final output will be:
A | B | c
--------------------
5 | 0 | 0
5 | 1 | 45
15 | 1 | 45
10 | 1 | 45
10 | 1 | 45
20 | 2 | 50
15 | 2 | 50
10 | 2 | 50
5 | 3 | 25
10 | 3 | 25
10 | 4 | 20
20 | 0 | 0

Write 1s faster to col-rows based on positions in a list

I'm new to pandas. I'm using a dataframe to tally how many times two positions match.
Here is the code in question...right at the start. The "what am I trying to accomplish" below...
def crossovers(df, index):
# Duplicate the dataframe passed in
_dfcopy = df.copy(deep=True)
# Set all values to 0
_dfcopy[:] = 0.0
# change the value of any col/row where there's a shared SNP
for i in index:
for j in index:
if i == j: continue # Don't include self as a shared SNP
_dfcopy[i][j] = 1
# Return the DataFrame.
# Should only contain 0s (no shared SNP) or 1s ( a shared SNP)
return _dfcopy
QUESTION:*
The data is flipping all the 0s in a dataframe to 1s, for all the intersections of rows/columns in a list (see details below).
I.e. if the list is
_indices = [0,2,3]
...all the locations at (0,2); (0,3); (2,0); (2,3); (3,0); and (3,2) get flipped to 1s.
Currently I do this by iterating through the list recursively onto itself. But this is painfully slow...and I'm passing in 16 million lines of data (16 mil indices).
How can I speed up this overall process?
LONGER DESCRIPTION
I start with a dataframe called sharedby_BOTH similar to below, except much larger (70 cols x 70 rows)- I'm using it to tally occurrences of shared data intersections.
Rows (index) are labeled 0,1,2,3 & 4...70 - as are the columns. Each location contains a 0.
sharedby_BOTH
0 1 2 3 4 (more)
------------------
0 | 0 | 0 | 0 | 0 | 0
1 | 0 | 0 | 0 | 0 | 0
2 | 0 | 0 | 0 | 0 | 0
3 | 0 | 0 | 0 | 0 | 0
4 | 0 | 0 | 0 | 0 | 0
(more)
Then I have a list, which contains intersecting data.
_indices = [0,2,3 (more)] # for example
This means that 0, 2, & 3 all contain shared data. So, I pass it to crossovers which returns a dataframe with a "1" at the intersection places, obtaining this...
0 1 2 3 4 (more)
------------------
0 | 0 | 0 | 1 | 1 | 0
1 | 0 | 0 | 0 | 0 | 0
2 | 1 | 0 | 0 | 1 | 0
3 | 1 | 0 | 1 | 0 | 0
4 | 0 | 0 | 0 | 0 | 0
(more)
...where the shared data locations are (0,2),(0,3),(2,0),(2,3),(3,0),(3,2).
*Notice that self is not recognized [(0,0), (2,2), and (3,3) DO NOT have 1s] *
Then I add this to the original dataframe with this code (inside a loop)...
sharedby_BOTH = sharedby_BOTH.add(crossovers(sharedby_BOTH, _indices)
I repeat this in a loop...
for pos, pos_val in chrom_val.items(): # pos_val is a dict
_indices = [i for i, x in enumerate(pos_val["sharedby"]) if (x == "HET")]
sharedby_BOTH = sharedby_BOTH.add(crossovers(sharedby_BOTH, _indices))
The end result is that sharedby_BOTH will look like the following, if I added the three example _indices
sharedby_BOTH = sharedby_BOTH.add(crossovers(sharedby_BOTH, [0,2,3] ))
sharedby_BOTH = sharedby_BOTH.add(crossovers(sharedby_BOTH, [0,2,4] ))
sharedby_BOTH = sharedby_BOTH.add(crossovers(sharedby_BOTH, [0,2,3] ))
0 1 2 3 4 (more)
------------------
0 | 0 | 0 | 3 | 2 | 1
1 | 0 | 0 | 0 | 0 | 0
2 | 3 | 0 | 0 | 2 | 1
3 | 2 | 0 | 2 | 0 | 0
4 | 1 | 0 | 1 | 0 | 0
(more)
...where, amongst the three indices passed in...
0shared data with 2 a total of three times so (0,2) and (2,0) totaled three.
0shared data with 3 twice so (0,3) and (3,0) total two.
0shared data with 4 only once, so (0,4) and (4,0) total one.
I hope this makes sense :)
EDIT
I did try the following...
addit = pd.DataFrame(1, index=_indices, columns=_indices)
sharedby_BOTH = sharedby_BOTH.add(addit)
BUT...then any locations within sharedby_BOTH that DID NOT HAVE SHARED DATA ended up as NAN
I.e...
sharedby_BOTH = pd.DataFrame(0, index=[x for x in range(4)], columns=[x for x in range(4)])
_indices = [0,2,3 (more)] # for example
addit = pd.DataFrame(1, index=_indices, columns=_indices)
sharedby_BOTH = sharedby_BOTH.add(addit)
0 1 2 3 4 (more)
------------------
0 | NAN | NAN | 1 | 1 | NAN
1 | NAN | NAN | NAN | NAN | NAN
2 | 1 | NAN | NAN | 1 | NAN
3 | 1 | NAN | 1 | NAN | NAN
4 | NAN | NAN | NAN | NAN | NAN
(more)
I'd organize it with numpy slice assignment and the handy np.triu_indices function. It returns the row and column indices of the upper triangle. I make sure to pass k=1 to ensure I skip the diagonal. When I slice assign, I make sure to use both i, j and j, i to get upper and lower
triangles.
def xover(n, idx):
idx = np.asarray(idx)
a = np.zeros((n, n))
i_, j_ = np.triu_indices(len(idx), 1)
i = idx[i_]
j = idx[j_]
a[i, j] = 1
a[j, i] = 1
return a
pd.DataFrame(xover(len(df), [0, 2, 3]), df.index, df.columns)
0 1 2 3
0 0.0 0.0 1.0 1.0
1 0.0 0.0 0.0 0.0
2 1.0 0.0 0.0 1.0
3 1.0 0.0 1.0 0.0
Timings
%timeit pd.DataFrame(xover(len(df), [0, 2, 3]), df.index, df.columns)
10000 loops, best of 3: 192 µs per loop
%%timeit
for i,j in product(li,repeat=2):
if i != j:
ndf.loc[i,j] = 1
100 loops, best of 3: 6.8 ms per loop
You can use itertools product and loc for assignment i.e
from itertools import product
li = [ 0,2,3]
ndf = df.copy()
for i,j in product(li,repeat=2):
if i != j:
ndf.loc[i,j] = 1
0 1 2 3 4
0 0 0 1 1 0
1 0 0 0 0 0
2 1 0 0 1 0
3 1 0 1 0 0
4 0 0 0 0 0

Add non-existent numbers with a zero value in Excel

I have lot of sequential data in Excel.
The missing number in the sequence for Column A, should be populated with zero value in column B.
What formula i could use for this?
Data in excel sheet as below:
A | B
0 | 4
1 | 6
4 | 4
6 | 7
Expected output:
A | B
0 | 4
1 | 6
2 | 0
3 | 0
4 | 4
5 | 0
6 | 7
In B1 of your second table
=IFERROR(VLOOKUP(A1,original_table,2,0),0)
and drag down. Replace original_table with a reference to your first table

Identify first occurrence of an entry in a list, if certain criteria is met (EXCEL)

I have spent too much time trying to figure our the question imposed, in the title of this question, and am reaching out for some help.
I would like to output an entry of 1 for the first occurrence when criteria is equal to AR and output an entry of 1 for the first occurrence on a column when criteria is equal to SFR on a seperate column.
For example the columns I'd want in the following table are the columns titled AR and SFR
|Property Name | product | company | AR | SFR |
|---------------|---------|---------|----|-----|
|orange grove | 2 | SFR | 0 | 1 |
|orange grove | 1 | AR | 1 | 0 |
|orange grove | 6 | AR | 0 | 0 |
|garden court | 2 | SFR | 0 | 1 |
|garden court | 1 | AR | 1 | 0 |
|chimney sweeps | 6 | AR | 1 | 0 |
|chimney sweeps | 2 | SFR | 0 | 1 |
|chimney sweeps | 1 | AR | 0 | 0 |
|chimney sweeps | 4 | SFR | 0 | 0 |
|downing apts | 2 | SFR | 0 | 1 |
|downing apts | 1 | SFR | 0 | 0 |
|downing apts | 6 | AR | 1 | 0 |
I tried implementing countif within my formula and only taking into consideration when my output is a result of 1, then nesting the formula for criteria "AR" or "SFR" with the result of my countif, if the criteria is satisfied, but the table isn't organized so that AR comes first in the table row and vice versa. Here is an example of my formula:
=IF(E2="AR", IF(COUNTIF($C$2:C2,C2)=1,1,""), "")
Sometimes I'll have instances where the company name occurs but the count isn't 1. Take for example orange grove. Orange grove's count would be 1, 2, 3 consecutively, for each +1 instance. If my count is 1 and SFR, my formula would output a 1 but if my criteria is AR and the count is not 1 (it's actually 2 or 3), I don't get a first occurrence output per my criteria of AR.
I saw there may be a way to utilize sumproduct but I am not familiar with it.
Could anyone please help. Anything would be appreciated!
It would guanrantee you a result if you could CONCATENATE the Property Name and Company and then input the below mentioned formula into AR and SFR cells.
Insert a column and use =CONCATENATE(A2," ",C2) to merge Product
Name and Company. It should give you result in space delimited
format. For eg. orange grove SFR, chimney sweeps AR, etc.
Enter this formula in Cell E2 which belongs under AR column: =IF(TRIM(RIGHT($D2,3))=E$1,IF(OR(COUNTIF($D:$D,$D2)=1,MATCH($D2,$D:$D,0)>=ROW($D2)),1,0),0)
Drag the formula left onto Cell F2 to apply to SFR column and then to the bottom of each columns to apply for all records/rows.
In the end you will have the following result as expected:
Table:
R/C A B C D E F
01 Property Name product company Merged PN + C AR SFR
-------------------------------------------------------------------------
02 orange grove 2 SFR orange grove SFR 0 1
03 orange grove 1 AR orange grove AR 1 0
04 orange grove 6 AR orange grove AR 0 0
05 garden court 2 SFR garden court SFR 0 1
06 garden court 1 AR garden court AR 1 0
07 chimney sweeps 6 AR chimney sweeps AR 1 0
08 chimney sweeps 2 SFR chimney sweeps SFR 0 1
09 chimney sweeps 1 AR chimney sweeps AR 0 0
10 chimney sweeps 4 SFR chimney sweeps SFR 0 0
11 downing apts 2 SFR downing apts SFR 0 1
12 downing apts 1 SFR downing apts SFR 0 0
13 downing apts 6 AR downing apts AR 1 0
Explanation:
We merged Product Name & Company to identify unique records.
Then we created a condition to check for last 3 characters to match our column name: =IF(TRIM(RIGHT($D2,3))=E$1
If the above condition is true which means that we are in the correct Company column, we can now proceed to check if the Product Name instance is first or not. If not, then we are in the wrong column so just print 0.
As you mentioned there can be either a unique combination or multiple combinations of Product Name & Company. Therefore there are 2 conditions:
a. When combination is unique i.e. count is equal to 1: COUNTIF($D:$D,$D2)=1
b. When combination is duplicate but row number of first instance is less than equal to current row number: MATCH($D4,$D:$D,0)>=ROW($D4)
If either of the above 2 conditions are TRUE then the record is first instance and hence print 1, else it is not and hence print 0.
Hope it helped.
Assuming Column D and E are where your AR and SFR columns are respectively, try:
Table Structure:
AR SFR
AR 1 0
AR 0 0
AR 0 0
AR 0 0
AR 0 0
SFR 0 1
Cell D2 Would be (assuming AR is your column Header for column D)
=IF(C2=D1,1,0)
E2 Would be:
=IF(C2=E1,1,0)
For D3, then drag down for each cell in column D
=IF(SUM(D$2:D2)=0,IF($C3=D$1,1,0),0)
For E3, then drag down for each cell in column E
IF(SUM(E$2:E2)=0,IF($C3=E$1,1,0),0)
Thanks!
Ryan

Resources