Replacing cells with cumulative count - if-statement

is there a way to create a formula, that will cumulatively count cells with an x inside and output the result as a new range?
So this would be achieved:
It would also be completely fine if the cumulative count was in all cells (so the result would look like so):

Assuming you're looking for the formula to go in cells A6:C9 -
Put this formula into cell A6 and copy across/down:
=COUNTA($A1:A1)
If you want to specify the criteria for what increments the counter:
=COUNTIF($A1:A1,"x")

I ended up using this:
=ARRAYFORMULA(if(A2:C2="x",COUNTIFS(A2:C2,A2:C2,COLUMN(A2:C2),"<="&COLUMN(A2:C2)),))
which is a derived version of a formula in this tutorial:
https://infoinspired.com/google-docs/spreadsheet/running-count-in-google-sheets/

try:
=ARRAYFORMULA(IF(A1:C4="",,
IF(A1:A4<>"", COLUMN(A:C),
IF(B1:B4<>"", COLUMN(A:C)-1,
COLUMN(A:C)-2))))
update:
=ARRAYFORMULA(IF(A1:AI4="",,
IF(A1:A4<>"", COLUMN(A:AI), IF(B1:B4<>"", COLUMN(A:AI)-1,
IF(C1:C4<>"", COLUMN(A:AI)-2, IF(D1:D4<>"", COLUMN(A:AI)-3,
IF(E1:E4<>"", COLUMN(A:AI)-4, IF(F1:F4<>"", COLUMN(A:AI)-5,
IF(G1:G4<>"", COLUMN(A:AI)-6, IF(H1:H4<>"", COLUMN(A:AI)-7,
IF(I1:I4<>"", COLUMN(A:AI)-8, IF(J1:J4<>"", COLUMN(A:AI)-9,
IF(K1:K4<>"", COLUMN(A:AI)-10, IF(L1:L4<>"", COLUMN(A:AI)-11,
IF(M1:M4<>"", COLUMN(A:AI)-12, IF(N1:N4<>"", COLUMN(A:AI)-13,
IF(O1:O4<>"", COLUMN(A:AI)-14, IF(P1:P4<>"", COLUMN(A:AI)-15,
IF(Q1:Q4<>"", COLUMN(A:AI)-16, IF(R1:R4<>"", COLUMN(A:AI)-17,
IF(S1:S4<>"", COLUMN(A:AI)-18, IF(T1:T4<>"", COLUMN(A:AI)-19,
IF(U1:U4<>"", COLUMN(A:AI)-20, IF(V1:V4<>"", COLUMN(A:AI)-21,
IF(W1:W4<>"", COLUMN(A:AI)-22, IF(X1:X4<>"", COLUMN(A:AI)-23,
IF(Y1:Y4<>"", COLUMN(A:AI)-24, IF(Z1:Z4<>"", COLUMN(A:AI)-25,
IF(AA1:AA4<>"", COLUMN(A:AI)-26, IF(AB1:AB4<>"", COLUMN(A:AI)-27,
IF(AC1:AC4<>"", COLUMN(A:AI)-28, IF(AD1:AD4<>"", COLUMN(A:AI)-29,
IF(AE1:AE4<>"", COLUMN(A:AI)-30, IF(AF1:AF4<>"", COLUMN(A:AI)-31,
IF(AG1:AG4<>"", COLUMN(A:AI)-32, IF(AH1:AH4<>"", COLUMN(A:AI)-33,
COLUMN(A:AI)-34))))))))))))))))))))))))))))))))))))

Related

Google Sheets multiple conditions with nested If statements?

I'm really struggling with this formula, but I think I'm overthinking it. I'm not sure what's the proper way to write this:
What I want to happen is:
If cell A1 = John AND
cell B1 = Buyer
then take the dollar amount in C1 and multiply it by 10%
ELSE
If cell A1 = John AND
cell B1 = Seller
then take the amount in C1 and multiple it by 25% BUT
If A1 is NOT equal to John, then return a currency value of $0
I sincerely appreciate any help. Thank you!
Lisa
Try this:
=if(A1="John",ifs(B1="Buyer",C1*10%,B1="Seller",C1*25%),0)
or try:
=IF(A1="John",
IF(B1="Buyer", C1*10%,
IF(B1="Seller", C1*25%, )), 0)
or:
=IF((A1="John")*(B1="Buyer"), C1*10%,
IF((A1="John")*(B1="Seller"), C1*25%, 0))

Convert List into a integer or float

so i'm having a problem converting to a list to integer or float. what i mean in converting is like this. i have a list that has one element.
newList = ['2.0G']
i want that to convert into this
numFloat = 2.0
or
numInt = 2
i tried regex to extract the number from string so i can assign it to another variable
firstVariable = re.findall(r"[-+]?\d*\.\d+|\d+", newList[0])
i have to keep calling the index in firstVariable to access the 2.0 or 2
Does this work ?
newList = ['2.0G']
numFloat = ''.join([i for i in newList[0] if i.isdigit() or i == '.'])
print(numFloat)
The above code will print 2.0 to the console.
What I am doing here, is that I am going through each and every item in the first item of the list , and then checking if its a digit or a .. If its not any one of these I move to the next character.
Please note that I am doing newList[0] because you mentioned that your list has only one element.
Please let me know if this works for you.
Thanks.

libreoffice calc: third argument to be checked

I had troubles to find out how to test a third value that sometimes is present in my spreadsheet.
This i have and it works for 2 Values (means the "standart"-case or if "I" is a numeric Value...):
=IF(ISNUMBER(I7); I7*J7; E7*G7)
In the column C, if a third value with the name: "freddyfox" is present, then do this math instead: E/G (divide, ...as said instead of the above (I * J or else E * G).
i have tryed things like this...
=IF(ISNUMBER(I7); I7*J7; E7*G7, IF(AND(C7=freddyfox); E7/G7) ))
There is no need to use AND. Instead, put the second IF in the 'true' result of the first IF.
=IF(ISNUMBER(I7); IF(C7="freddyfox"; E7/G7; I7*J7); E7*G7)
This yields E7/G7 when ISNUMBER(I7) and C7="freddyfox". If ISNUMBER(I7) but not C7="freddyfox" then it yields I7*J7, and if neither is true, then it yields E7*G7.
EDIT:
Here is the formula requested in the comment.
=IF(C7="freddyfox"; E7/G7; IF(ISNUMBER(I7); I7*J7; E7*G7))

Pandas apply function taking up to 10min (numba doesnot help)

I have got a very simple function to apply to each row of my dataframe:
def distance_ot(fromwp,towp,pl,plee):
` if fromwp[0:3]==towp[0:3]:
sxcord=pl.loc[fromwp,"XCORD"]
sycord=pl.loc[fromwp,"YCORD"]
excord=pl.loc[towp,"XCORD"]
eycord=pl.loc[towp,"YCORD"]
x=np.abs(excord-sxcord); y=np.abs(eycord-sycord)
distance=x+y
return distance
else:
x1=np.abs(plee.loc[fromwp[0:3],"exitx"]-pl.loc[fromwp,"XCORD"])
y1=np.abs(plee.loc[fromwp[0:3],"exity"]-pl.loc[fromwp,"YCORD"])
x2=np.abs(plee.loc[fromwp[0:3],"exitx"]-plee.loc[towp[0:3],"entryx"])
y2=np.abs(plee.loc[fromwp[0:3],"exity"]-plee.loc[towp[0:3],"entryy"])
x3=np.abs(plee.loc[towp[0:3],"entryx"]-pl.loc[towp,"XCORD"])
y3=np.abs(plee.loc[towp[0:3],"entryy"]-pl.loc[towp,"YCORD"])
distance=x1+x2+x3+y1+y2+y3
return distance
With this line it is called:
pot["traveldistance"]=pot.apply(lambda row: distance_ot(fromwp=row["from_wpadr"],towp=row["to_wpadr"],pl=pl,plee=plee),axis=1)
Where: fromwp and towp are both strings and xcord and ycord are floats. I tried using numba but for some reasons it does not enhance this performance. Any suggestions?
Thanks to caiohamamura hint hereby the solution:
distance_ot(pl=pl,plee=plee)
pot.ix[pot.from_wpadr.str[0:3]==pot.to_wpadr.str[0:3],"traveldistance"]=pot["distance1"]
pot.ix[pot.from_wpadr.str[0:3]!=pot.to_wpadr.str[0:3],"traveldistance"]=pot["distance2"]
def distance_ot(pl,plee):
from_df = pl.loc[pot["from_wpadr"]]
to_df = pl.loc[pot["to_wpadr"]]
sxcord=from_df["XCORD"].values
sycord=from_df["YCORD"].values
excord=to_df["XCORD"].values
eycord=to_df["YCORD"].values
x=np.abs(excord-sxcord); y=np.abs(eycord-sycord)
pot["distance1"]=x+y
from_df2=plee.loc[pot["from_wpadr"].str[0:3]]
to_df2=plee.loc[pot["to_wpadr"].str[0:3]]
x1=np.abs(from_df2["exitx"].values-from_df["XCORD"].values)
y1=np.abs(from_df2["exity"].values-from_df["YCORD"].values)
x2=np.abs(from_df2["exitx"].values-to_df2["entryx"].values)
y2=np.abs(from_df2["exity"].values-to_df2["entryy"].values)
x3=np.abs(to_df2["entryx"].values-to_df["XCORD"].values)
y3=np.abs(to_df2["entryy"].values-to_df["YCORD"].values)
pot["distance2"]=x1+x2+x3+y1+y2+y3
Vectorize the distance_ot function to calculate all distances at once. I would begin populating a from_df and a to_df like the following:
import numpy as np
from_df = pl.loc[np.in1d(pl.loc.index, pot["from_wpadr"])
to_df = pl.loc[np.in1d(pl.loc.index, pot["to_wpadr"])
Then you can continue as in your function:
sxcord=from_df["XCORD"]
sycord=from_df["YCORD"]
excord=to_df["XCORD"]
eycord=to_df["YCORD"]
x=np.abs(excord-sxcord); y=np.abs(eycord-sycord)
distances=x+y
This will calculate all the distances at once. Your if clause can also be vectorized, your results will be calculated into different arrays, those which match the if clause and those that doesn't, you just have to keep track of the boolean array, so you can put them together in the dataframe afterwards:
first_three_equals = np.char.ljust(pot["from_wpadr"].values.astype(str), 3) \
== np.char.ljust(pot["to_wpadr"].values.astype(str), 3)

How to normalize Excel cell data

I am getting the data from an external source, and I need to normalize the cells.
like this:
A
32.1342244,31.1322214,0
35.12331299999999,12.14324553333333,0
..
..
And I would like Row A to be in the form of two numbers (instead of three) and with the mantissa with 8 digits max. so the above data would look like this:
A
32.1342244,31.1322214
35.12331299,12.14324553
..
..
How can I do this? Is there a way to use a regexp for that?
Can this be done in a way to simply sanitize the data and not copy it from one place to another?
Select the cells you wish to process and run this tiny macro:
Sub Normalize()
Dim r As Range, ary
For Each r In Selection
ary = Split(r.Value, ",")
ary(0) = Format(ary(0), "0.00000000")
ary(1) = Format(ary(1), "0.00000000")
r.Value = ary(0) & "," & ary(1)
Next r
End Sub