LibreOffice/Excel Table Calculation Formula - excel

I have three Columns in one sheet. Col1 Have Combination Of Col2 Values, I need to replace Col1 Value as equal Of COl2 = col3 Value.
Is there Any Formula to do this in LibreOffice Calculation.
Actual Table:
Col1 | col2 | Col3
A | A | X
C | B | Y
A | C | Z
B | |
A | |
B | |
C | |
A | |
C |
B |
Expected Output:
Col1 | col2 | Col3
X | A | X
Z | B | Y
X | C | Z
Y | |
X | |
Y | |
Z | |
X | |
Z |
Y |
Thanks In Advance, I am struggling For long days in this.

Basically it's a work around. You would like to change A->X, B->Y and C->Z in col1. Create a col4 with the formula
=CHAR(CODE(A1)+23)
This offsets the A by 23 characters that will be X and therefore, B and Cs with Y and Z.

Related

Identify the parent and children value in the dataframe

I spend almost half of my day trying to solve this...
I want to identify the value in parent and child columns and change it to rows.
The value has a tree structure in that the parent node becomes the child node, and the child node becomes the parent node at the next step.
My sample data looks like.
| Parent | Child |
--------------------------
0 | a b
1 | b c
2 | b d
3 | c e
4 | c f
5 | f g
6 | d h
and I want to change this like,
| Col1 | Col2 | Col3 | Col4 | Col5 | Col6 |
----------------------------------------------------------
0 | a | b | c | f | g | nan |
1 | a | b | c | e | nan | nan |
2 | a | b | d | h | nan | nan |
I have tried doing the loop for searching the next items, but it does not work.
Any help would be appreciated.
You can approach this using a graph and networkx.
You graph is:
Create all edges, find the roots and leafs and compute the paths with all_simple_paths:
import networkx as nx
G = nx.from_pandas_edgelist(df, source='Parent', target='Child',
create_using=nx.DiGraph)
roots = [n for n,d in G.in_degree() if d==0]
leafs = [n for n,d in G.out_degree() if d==0]
df2 = pd.DataFrame([l for r in roots for l in nx.all_simple_paths(G, r, leafs)])
output:
0 1 2 3 4
0 a b c e None
1 a b c f g
2 a b d h None

Sorting rows in pandas first by timestamp values and then by giving particular order to categorical values of a column

I have a pandas dataframe which has a column "user" containing categorical values(a,b,c,d). I only care about the ordering of two categories in ascending order (a, d). So (a,b,c,d) and (a,c,b,d) both are fine for me.
How to create the ordering is the first part of the question?
Secondly I have another column which contains "timestamps". I want to order my rows first by "timestamps" and then for the rows with same timestamps I want to sort with the above ordering of categorical values.
Lets say my data frame looks like this.
+-----------+------+
| Timestamp | User |
+-----------+------+
| 1 | b |
| 2 | d |
| 1 | a |
| 1 | c |
| 1 | d |
| 2 | a |
| 2 | b |
+-----------+------+
I want first this kind of sorting to happen
+-----------+------+
| Timestamp | User |
+-----------+------+
| 1 | b |
| 1 | a |
| 1 | c |
| 1 | d |
| 2 | d |
| 2 | a |
| 2 | b |
+-----------+------+
Followed by the categorical ordering of "user"
+-----------+------+
| Timestamp | User |
+-----------+------+
| 1 | a |
| 1 | b |
| 1 | c |
| 1 | d |
| 2 | a |
| 2 | b |
| 2 | d |
+-----------+------+
OR
+-----------+------+
| Timestamp | User |
+-----------+------+
| 1 | a |
| 1 | c |
| 1 | b |
| 1 | d |
| 2 | a |
| 2 | b |
| 2 | d |
+-----------+------+
As you can see the "c" and "b"'s order do not matter.
You can specify order in ordered categorical by categories and then call DataFrame.sort_values:
df['User'] = pd.Categorical(df['User'], ordered=True, categories=['a','b','c','d'])
df = df.sort_values(['Timestamp','User'])
print (df)
Timestamp User
2 1 a
0 1 b
3 1 c
4 1 d
5 2 a
6 2 b
1 2 d
If there is many values of User is possible dynamically create categories:
vals = ['a', 'd']
cats = vals + np.setdiff1d(df['User'], vals).tolist()
print (cats)
['a', 'd', 'b', 'c']
df['User'] = pd.Categorical(df['User'], ordered=True, categories=cats)
df = df.sort_values(['Timestamp','User'])
print (df)
Timestamp User
2 1 a
4 1 d
0 1 b
3 1 c
5 2 a
1 2 d
6 2 b

Using openoffice Calc or Excel to find variables which occur in multiple columns?

I need a way to check if a variable occurs in all of my columns at least once.
Example with 3 columns, while Occu in Col1-3 checks if the variable which is listed in Col3 exists in every other column. Only for a the formula should return "true".
How can I achieve this? I tried using "countif" but unfortunately this only works for a maximum of 2 columns.
+------+------+------+----------------+
| Col1 | Col2 | Col3 | Occu in Col1-3 |
+------+------+------+----------------+
| a | a | f | false |
| b | c | a | true |
| c | d | e | false |
+------+------+------+----------------+
Any help is upvoted, thanks
Multiply another COUNTIF statement for each column:
=COUNTIF(A$1:A$3,"=" & C1) * COUNTIF(B$1:B$3,"=" & C1)

Automatically calculate (or delete) rows in Excel when first column is changing

I have a big table, where first columns X is "input column" and range it's changing.
Y - There are more formulas and functions (Vlookup) and 1st column X is a lookup value, and then other columns are calculated from other sheets.
| A | B | C | D | E
1 | X | Y | Y | Y | Y
2 | X | Y | Y | Y | Y
3 | X | Y | Y | Y | Y
4 | X | Y | Y | Y | Y
I am inserting (and deleting) more X values (actual data) and then I use "double click" for all other Y columns to be calculated, BUT it's not good because the X range is not the same. I tried to convert it to table "Ctrl-T", but it's not working very good for me. Maybe I don't use it properly.
Problem:
If I paste a new X column, I need other Y columns to be automatically calculated OR if I delete few X rows, other Y should be also deleted. Now I get something like this:
| A | B | C | D | E
1 | X | Y | Y | Y | Y
2 | X | Y | Y | Y | Y
3 | | N/A | N/A | N/A | N/A
4 | | N/A | N/A | N/A | N/A
or:
| A | B | C | D | E
1 | X | Y | Y | Y | Y
2 | X | Y | Y | Y | Y
3 | X | | | |
What I need:
If I remove X value I need automatically disappear Y values:
| A | B | C | D | E
1 | X | Y | Y | Y | Y
2 | X | Y | Y | Y | Y
If I add X value I need automatically calculate Y values:
| A | B | C | D | E
1 | X | Y | Y | Y | Y
2 | X | Y | Y | Y | Y
3 | X | Y | Y | Y | Y
Hope it's clear, thank you!
For Y Columns, you can add "IF" FORMULA
=if(A1>0,*Y COLUMN FORMULA*,"")
try changing formula to
=iferror(*Y formula,"")
or if it's still slow and if you are changing only X Columns
you can use below code
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Count = 1 Then 'CHECK IF THERE IS ANY CHANGE ON X COLUMN
If Target.Value = Empty Then 'CHECK IF X COLUMN HAS BEEN DELETED
Rows(Target.Row).Delete 'IF X COLUMN IS DELETED, DELETS WHOLE ROW
Else
Cells(Target.Row - 1, 2).Resize(1, 4).Copy Cells(Target.Row, 2).Resize(1, 4) 'IF X COLUMN IS ENTERED OR MODIFIED COPIES ABOVE Y COLUMN FORMULAS
End If
End If
End Sub

Insert new columns only when ID is the same in Excel

I have 2 worksheets with similar table structures which looks like this:
| ID | A | B | C |
+--------+-------+-------+-------+
| 1 | x | x | x |
| 4 | x | x | x |
| 12 | x | x | x |
| 3 | x | x | x |
| |
| ... (thousands of rows)
where x are values. Is it possible to create a new table (or worksheet) combining the two worksheets only where the ID from Worksheet1 is the same (similar to a SQL query) so that the resulting table will be like:
| ID | A | B | C | D | E | F |
+--------+-------+-------+-------+-------+-------+-------+
| 1 | x | x | x | x | x | x |
| 4 | x | x | x | x | x | x |
| 12 | x | x | x | x | x | x |
| 3 | x | x | x | x | x | x |
| |
| etc...
Note that the contents of Worksheet1 is added to and not subtracted from. Is VBA necessary or can it be done with a formula? Thank you.
You can use vlookup to solve this.
vlookup searches for id in sheet2 and returns corresponding value in your specified column number of the selected table.

Resources