How to use arrayformula with GCD? [duplicate] - google-sheets

This question already has an answer here:
How to use arrayformula with formulas that do not seem to support arrayformulas?
(1 answer)
Closed 4 months ago.
I'm trying to use this formula to determine if Column A is a multiple of 5, but the arrayformula isn't expanding.
=ARRAYFORMULA(IF(GCD(5,A1:A)=5,TRUE,FALSE))
GCD doesn't seem to support arrayformula. How to make it work with ARRAYFORMULA?
Column C is the result of using autofill.
A
B[Arrayformula]
C[DragFill]
1
FALSE
FALSE
2
FALSE
3
FALSE
4
FALSE
5
TRUE
6
FALSE
7
FALSE
8
FALSE
9
FALSE
10
TRUE
62
FALSE
34
FALSE
22
FALSE
75
TRUE
34
FALSE
46
FALSE
30
TRUE
21
FALSE
92
FALSE
56
FALSE

try:
=BYROW(A1:A20, LAMBDA(x, GCD(5, x)=5))
or just:
=INDEX(MOD(A1:A20, 5)=0)

Related

Intercalate columns when they are in pairs

Using this table:
A
B
C
D
1
2
3
4
5
6
7
8
9
10
11
12
In Google Sheets if I do this here in column E:
={A1:B3;C1:D3}
Teremos:
E
F
1
2
5
6
9
10
3
4
7
8
11
12
But the result I want is this:
E
F
1
2
3
4
5
6
7
8
9
10
11
12
I tried multiple options with FLATTEN, but none of them returned what I wanted.
Well you can try:
=WRAPROWS(TOCOL(A1:D3),2)
You could try with MAKEARRAY
=MAKEARRAY(ROWS(A1:D3)*2,2,LAMBDA(r,c,INDEX(FLATTEN(A1:D3),c+(r-1)*2)))
GENERAL ANSWER
For you or anyone else: to do something similar but with a variable number of columns of origin or of destination, you can use this formula. Changing the range and amount of columns at the end of LAMBDA:
=LAMBDA(range,cols,MAKEARRAY(ROWS(range)*ROUNDUP(COLUMNS(range)/cols),cols,LAMBDA(r,c,IFERROR(INDEX(FLATTEN(range),c+(r-1)*cols)))))(A1:D3,2)
you can do:
={FLATTEN({A1:A3, C1:C3}), FLATTEN({B1:B3, D1:D3})}
for more columns, it could be automated with MOD

How To Skip Down by 1 Row/Cell The Formula Output and Remove The Last Sequential Output Before 1's Google Sheets?

I've got these 3 groups of data in range F2:G22 as below
(3 groups as minimal example, in reality many thousands of groups, and recurrent similar datasets expected in the future):
I need to number each group's rows sequentially, starting over at 1 at each new group.
The expected result would be like in range E1:E22.
I tried the following formula n cell C2 , then in cell D3:
=INDEX(IF(A2:A22="",COUNTIFS(B2:B22&A2:A22, B2:B22&A2:A22, ROW(B2:B22), "<="&ROW(B2:B22)),1))
In C2:
In D3:
That fixed partially the sequence issue, but there's still 2 issues I can't find remedy for.
1st remaining issue:
I'd prefer not having to manually do the C2 to D3 step each time I get new similar data (but would accomodate if there's no simple solution to this issue).
Is there a simple way to modify the formula to make it output the correct sequencing from C2 ?
2nd remaining issue:
At rows 7, 14 and 23 there still remain unecessary ending numbering for these intermediary rows in D7 , D14 , and D23:
I could only think of an extra manual step of filtering out the non-blank rows in Column A to fix this 2nd issue (i.e. Highlighting Column A > Data tab > Create Filter > Untick all > Tick Blanks > Copy All > Paste In new Sheet).
But would there be a way to do it in the same formula? I'm not seeing the way to add the proper filter or using another method in the formula.
Any help is greatly appreciated.
EDIT (Sorry for Forgotten Sample):
Formula Input A
Formula Input B
Formula Output 1
Formula Output 2
EXPECTED RESULT
rockinfreakshow
ztiaa
DATA
DATA BY GROUP
7
1
1
7
7
2
1
1
1
2
Element-1
Group-1
7
3
2
2
2
3
Element-2
Group-1
7
4
3
3
3
4
Element-3
Group-1
7
5
4
4
4
5
Element-4
Group-1
8
1
5
6
8
8
2
1
1
1
7
Element-1
Group-2
8
3
2
2
2
8
Element-2
Group-2
8
4
3
3
3
9
Element-3
Group-2
8
5
4
4
4
10
Element-4
Group-2
8
6
5
5
5
11
Element-5
Group-2
8
7
6
6
6
12
Element-6
Group-2
9
1
7
13
9
9
2
1
1
1
14
Element-1
Group-3
9
3
2
2
2
15
Element-2
Group-3
9
4
3
3
3
16
Element-3
Group-3
9
5
4
4
4
17
Element-4
Group-3
9
6
5
5
5
18
Element-5
Group-3
9
7
6
6
6
19
Element-6
Group-3
9
8
7
7
7
20
Element-7
Group-3
9
9
8
8
8
21
Element-8
Group-3
9
Can you try:
=INDEX(LAMBDA(y,z,
IF(LEN(z),COUNTIFS(y,y,ROW(z),"<="&ROW(z)),))
(LOOKUP(ROW(G2:G),FILTER(ROW(G2:G),BYROW(G2:G,LAMBDA(z,IF(z<>OFFSET(z,-1,0),row(z),0))))),G2:G))
You can simply use SCAN.
=SCAN(,G2:G,LAMBDA(a,c,IF(c="",,a+1)))
Sample sheet

How to separate numbers from string and the number should appear separately in separate cells in Google Sheets

I have a data in "A" column like as below starting from A2 cell.
7cz2
6789efg
abc890
34l1
78kk
88
63
What I need is
7
2
6
7
8
9
8
9
0
3
4
7
8
8
8
6
3.
I do have applied =VALUE(REGEXREPLACE(A2,"\D+", "")) but is is giving me data like
72
6789
890
34
78.
Is there a way to solve this.Thanks in advance.
Use 2 formulas.
join and replace letters in cell C1:
=REGEXREPLACE(JOIN("",A1:A7),"[A-Za-z]","")
split by symbol:
=TRANSPOSE(REGEXEXTRACT(C1,REPT("(.)",LEN(C1))))
The result:

join DataFrames on a partially matching index

I'm trying to find a more elegant way to join two DataFrames where the index levels of one DF are a partial subset of the index levels of the other DF. This is a very common operation in SQL and I'm surprised to find it's so difficult to do with pandas:
Here's an example:
import pandas as pd
df = pd.DataFrame(
{
2012:[4,5,8,9],
2013:[1,2,4,7],
2014:[6,5,4,3],
},
index= pd.MultiIndex.from_tuples([('apples',False),('bananas',False),('oranges',True),('lemons',True)], names=('fruit','citrus'))
)
=>
2012 2013 2014
fruit citrus
apples False 4 1 6
bananas False 5 2 5
oranges True 8 4 4
lemons True 9 7 3
[4 rows x 3 columns]
Now I want to know the highest number of each fruit sold in a given year:
fruit_max_by_date = df.max(axis=1).to_frame()
citrus_max_by_date = fruit_max_by_date.max(level='citrus')
citrus_max_by_date.columns = [1]
=>
fruit_max_by_date =
0
fruit citrus
apples False 6
bananas False 5
oranges True 8
lemons True 9
[4 rows x 1 columns]
citrus_max_by_date =
1
citrus
False 6
True 9
[2 rows x 1 columns]
So far so good. But now I try to join the latter two together:
fruit_max_by_date.join(citrus_max_by_date) =>
0 1
fruit citrus
apples False 6 NaN
bananas False 5 NaN
oranges True 8 NaN
lemons True 9 NaN
[4 rows x 2 columns]
Argh!! Because the index of the second table doesn't exactly match the index of the first table, the join fails. This seems totally contrary to the intuitive behavior of an SQL-like inner join.
All the workarounds below (especially the second) are butt-ugly and basically involve either throwing the index out the window, or manually broadcasting the index of one table. Is there a simpler way to do this?
Workaround: Expand the index of the smaller table through broadcasting
This is the least-ugly workaround I could come up with, but it's still quite bad in that it requires expanding the size of the second array for no good reason.
fruit_max_by_date.join(
citrus_max_by_date.reindex(fruit_max_by_date.index, level='citrus') ) =>
0 1
fruit citrus
apples False 6 6
bananas False 5 6
oranges True 8 9
lemons True 9 9
[4 rows x 2 columns]
Workaround: Truncate the index of the first table
This is horribly ugly, especially having to reassemble the index afterwards, but it works.
fruit_max_by_date \
.reset_index(level='fruit') \
.join(citrus_max_by_date) \
.set_index('fruit',append=True \
.reorder_levels((1,0)) =>
0 1
citrus fruit
False apples 6 6
bananas 5 6
True oranges 8 9
lemons 9 9
[4 rows x 2 columns]
Drop all pretense of using an index, and join without index
Okay, this is relatively straightforward, but what exactly is the point of having an index if you can't use it?
If using join — but not merge (FML!!) — there is another bizarre side effect: the joined-on column is reduplicated in the output:
fruit_max_by_date.reset_index().join(
citrus_max_by_date.reset_index(),
on='citrus', rsuffix='_' ) =>
fruit citrus 0 citrus_ 1
0 apples False 6 False 6
1 bananas False 5 False 6
2 oranges True 8 True 9
3 lemons True 9 True 9
[4 rows x 5 columns]
fruit_max_by_date.reset_index().merge(
citrus_max_by_date.reset_index(),
on='citrus' ) =>
fruit citrus 0 1
0 apples False 6 6
1 bananas False 5 6
2 oranges True 8 9
3 lemons True 9 9
[4 rows x 4 columns]

Using COUNTIFS on 3 different columns and then need to SUM a 4th column?

I have written this formula below. I do not know the correct part of this formula that will add the numbers I have in Column AB2:AB552. As it is, this formula is counting the number of cells in that range that has numbers in it, but I need it to total those numbers as my final result. Any help would be great.
=COUNTIFS(Cases!B2:B552,"1",Cases!G2:G552,"c*",Cases!X2:X552,"No",**Cases!AB2:AB552,">0"**)
Assuming you don't actually need the intermediate counts, the sumifs function should give you the final result:
=SUMIFS(Cases!AB2:AB552,Cases!B2:B552,1,Cases!G2:G552,"c",Cases!X2:X552,"No",Cases!AB2:AB552,">0")
Testing this with some limited data:
Row B G X AB
2 2 a No 10
3 1 c No 24
4 2 c No 4
5 1 c No 0
6 1 a Yes 9
7 2 c No 12
8 2 c No 6
9 2 b No 0
10 1 b No 0
11 1 a No 10
12 2 c No 6
13 1 c No 20
14 1 c No 4
15 1 b Yes 22
16 1 b Yes 22
the formula above returned 48, the sum of AB3, AB13, and AB14, which were the only rows matching all 4 criteria

Resources