Consider this table, in which Col A to C are given strings, and Col D to G are array formulas:
A
B
C
D
E
F
G
1
Tese
2
Afetado
REsp 1872759/SP
2020/0103921-2
REsp
Recurso especial
1872759
SP
3
Afetado
REsp 1895255/RS
2020/0237508-4
REsp
Recurso especial
1895255
RS
4
Afetado
REsp 1903883/CE
2020/0288219-1
REsp
Recurso especial
1903883
CE
Col D formula is:
=ARRAYFORMULA(IF(B2:B<>"", REGEXEXTRACT(B2:B,"[^[:space:]]+"),""))
Col E formula is:
=ARRAYFORMULA(IF(D2:D<>""; SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(D2:D, "EREsp", "Embargos em recurso especial"), "AREsp", "Agravo em recurso especial"), "CC", "Conflito de competência"), "REsp", "Recurso especial"), "Pet", "Petição"), "Rcl", "Reclamação"),""))
Col F formula is:
=ARRAYFORMULA(IF(B2:B<>"", REGEXREPLACE(B2:B,"\D+", ""),""))
Finally, Col G formula is:
=ARRAYFORMULA(IF(B2:B<>"", RIGHT(B2:B;2), ""))
All this to help Col H formula return this (note that row in which Col B string is null should return no result):
H
1
2
Recurso especial 1.872.759/SP (2020/0103921-2)
3
Recurso especial 1.895.255/RS (2020/0237508-4)
4
Recurso especial 1.903.883/CE (2020/0288219-1)
Col H formula is:
=ARRAYFORMULA(IF(E2:E<>""; E2:E&" "&TEXT(F2:F; "#,##0")&"/"&G2:G&" ("&TO_TEXT(C2:C)&")"; ""))
Is it possible to build a formula that merges those in Col D to H and achieve the same results?
This is the actual table: link
I can't tell from your example whether you're in a european sheet or not. If so, change all these commas to semicolons:
=ARRAYFORMULA(IF(B2:B="",,E2:E&" "&REPLACE(REPLACE(MID(B2:B,6,100),2,0,"."),6,0,".")&" ("&C2:C&")"))
Solved it!
ARRAYFORMULA(IF(B1:B<>"", SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(REGEXEXTRACT(B1:B,"[^[:space:]]+"), "EREsp", "Embargos em recurso especial"), "AREsp", "Agravo em recurso especial"), "CC", "Conflito de competência"), "REsp", "Recurso especial"), "Pet", "Petição"), "Rcl", "Reclamação")&" "&TEXT(REGEXREPLACE(B1:B,"\D+", ""), "#,##0")&"/"&RIGHT(B1:B,2)&" ("&TO_TEXT(C1:V)&")", ""))
But, if there is a way to simplify this formula, please share!
Related
My dataframe initially looks like this :
col1
col2
col3
a
b
c
a
b
d
a
b
nan
e
f
nan
Dataframe after replacing nan values with empty string:
col1
col2
col3
a
b
c
a
b
d
a
b
''
e
f
''
I want the dataframe to look like:
col1
col2
col3
a
b
c,d
e
f
import pandas as pd
import numpy as np
airflow_df = pd.read_csv("/Users/syc/astro-airflow-localdev/dags/export_dataframe.csv")
cols = ['trgt_schema','trgt_tbl','src_tbl']
subset_airflow = pd.DataFrame(airflow_df, columns=cols)
trgt_df_explode = subset_airflow.assign(src_tbl=t_df.src_tbl.str.split(',')).explode('src_tbl')
trgt_df_explode['src_tbl']=trgt_df_explode['src_tbl'].str.lower()
trgt_df_explode['src_tbl']=trgt_df_explode['src_tbl'].str.strip()
trgt_df_explode = trgt_df_explode.drop_duplicates()
trgt_df_explode_nan= trgt_df_explode.replace(np.nan, None , regex=True)
trgt_df_explode_nan['src_tbl'] = trgt_df_explode_nan.groupby(['trgt_schema','trgt_tbl'])['src_tbl'].transform(lambda x: ', '.join(map(str, x)))
trgt_df_explode = trgt_df_explode.drop_duplicates()
You can use dropna() function. More details: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html?
I have rows with start and end date. I need to repeat each row N times and increment new date column by one.
N = the number of days between the start date and en date
My table:
Column A
Start date
End date
A
10/09/2022
12/09/2022
B
15/09/2022
16/09/2022
C
08/09/2022
12/09/2022
The result I'd like to generate automatically (new row will often be added):
Column A
Start date
End date
Date
A
10/09/2022
12/09/2022
10/09/2022
A
10/09/2022
12/09/2022
11/09/2022
A
10/09/2022
12/09/2022
12/09/2022
B
15/09/2022
16/09/2022
15/09/2022
B
15/09/2022
16/09/2022
16/09/2022
C
08/09/2022
12/09/2022
08/09/2022
C
08/09/2022
12/09/2022
09/09/2022
C
08/09/2022
12/09/2022
10/09/2022
C
08/09/2022
12/09/2022
11/09/2022
C
08/09/2022
12/09/2022
12/09/2022
I hope my need is clear.
Thanks,
I've tried THIS, but the solution is for fixed N times while I need N to be dynamic.
UPDATE
I though it'll be easy to reproduce the solution to my exact need, but it's not the case... I've received two great solutions which work with my first example, but not the full need.
Here is an example of the exact need:
Col1
Col2
Col3
Col4
Col5
Col6
Col7
Col8
Start date
End date
Col11
Col12
Col13
Col14
Col15
Col16
Col17
Col18
Col19
A
B
C
D
E
F
G
H
10/09/2022
24/09/2022
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
A
05/10/2022
17/11/2022
D
E
F
G
H
I
J
K
L
try:
=QUERY(ARRAYFORMULA(SPLIT(FLATTEN(A2:A&"|"&B2:B&"|"&C2:C&"|"&MAP(B2:B,C2:C,LAMBDA(bx,cx,if(bx="",,TRANSPOSE(SEQUENCE(DATEDIF(bx,cx,"d")+1,1,bx,1)))))),"|")),"Select * Where Col4 IS NOT NULL")
-
Here's one way to do that using REDUCE.
=ARRAYFORMULA(
{A1:C1,"Date";
QUERY(
REDUCE(
{"","","",""},
BYROW(
FILTER(A2:C,A2:A<>"",B2:B<>"",C2:C<>""),
LAMBDA(row,JOIN("❄️",row))),
LAMBDA(acc,cur,
{acc;
LAMBDA(a,start,end,
LAMBDA(dif,
{IF(dif,{a,start,end}),SEQUENCE(MAX(dif),1,start)})
(SEQUENCE(end-start+1)))
(INDEX(SPLIT(cur,"❄️"),,1),
INDEX(SPLIT(cur,"❄️"),,2),
INDEX(SPLIT(cur,"❄️"),,3))})),
"OFFSET 1",0)})
UPDATE
=ARRAYFORMULA(
QUERY(
{TRIM(QUERY(
SPLIT(REDUCE(,
BYROW(A2:S3,LAMBDA(row,JOIN("❄",row)))&"♥"&J2:J3-I2:I3+1,
LAMBDA(
acc,cur,
{acc;
IF(SEQUENCE(INDEX(SPLIT(cur,"♥",,),,2)),
INDEX(SPLIT(cur,"♥",,),,1))})),"❄",,),
"OFFSET 1",0)),
QUERY(
FLATTEN(MAP(I2:I3,J2:J3,LAMBDA(start,end,SEQUENCE(1,end-start+1,start)))),
"WHERE Col1 IS NOT NULL")},
"SELECT Col"&JOIN(", Col",SEQUENCE(COLUMN(J1)))&
", Col"&COLUMNS(A2:S3)+1&
", Col"&JOIN(", Col",SEQUENCE(COLUMNS(A2:S3)-COLUMN(J1),1,COLUMN(J1)+1))))
I need to apply solution found in this question:
Using single formula to list unique items, count and sort numerically by descending order and then alphabetically for items with same count
This time adding a data validation menu for the search period.
I can do this for single cell but I'm not able to apply it to the solution above.
Formula for search period is this:
=COUNTIFS($B3:$B,"*apple*",$A3:$A,">="&TODAY()- VLOOKUP(
SUBSTITUTE(D2," ",""),
{"24HOURS",0;
"2DAYS",1;
"3DAYS",4;
"7DAYS",7;
"2WEEKS",14;
"1MONTH",30;
"3MONTHS",90;
"6MONTHS",180;
"1YEAR",365;
"2YEARS",730;
"3YEARS",1095;
"TOTAL",999999},
2,FALSE))
Formula taken from solution on question above:
=QUERY(B:B,"Select B, count(B) where B matches '^(?!(?:ITEMS|ExcludeB|ExcludeC)$).+' group by B order by count(B) DESC label count(B) ''")
Image to help clarify:
My dummy file:
https://docs.google.com/spreadsheets/d/1iB4BnqhTBVNuCCQ4GnEIu95gbzYb0T9H9A3Pi1W5AZk/edit?usp=sharing
Is such a thing possible? Any pointers on how this can be done? Thank you
In Excel (since you tagged it) you can use the following in Office 365:
=LET(a,A2:INDEX(B:B,LOOKUP(2,1/(A:A<>""),ROW(B:B))),
aa,INDEX(a,,1),
ab,INDEX(a,,2),
u,UNIQUE(INDEX(a,,2)),
c,COUNTIF(ab,u),
d,COUNTIFS(ab,u,
aa,">="&TODAY()
-VLOOKUP(SUBSTITUTE(D2," ",""),
{"24HOURS",0;
"2DAYS",1;
"3DAYS",4;
"7DAYS",7;
"2WEEKS",14;
"1MONTH",30;
"3MONTHS",90;
"6MONTHS",180;
"1YEAR",365;
"2YEARS",730;
"3YEARS",1095;
"TOTAL",999999},
2,0)),
SORT(CHOOSE({1,2,3},u,c,d),{2,1,1},{-1,1,1}))
This should do it.
=QUERY(
A:B,
"Select B, count(B)
where
B matches '^(?!(?:ITEMS|ExcludeB|ExcludeC)$).+' and
A >= date '"&
TEXT(
IFERROR(
VLOOKUP(
D2,
{"2 4 H O U R S",TODAY()-1;
"3 D A Y S",TODAY()-3;
"7 D A Y S",TODAY()-7;
"2 W E E K S",TODAY()-14;
"1 M O N T H",EDATE(TODAY(),-1);
"3 M O N T H S",EDATE(TODAY(),-3);
"6 M O N T H S",EDATE(TODAY(),-6);
"1 Y E A R",EDATE(TODAY(),-12);
"2 Y E A R S",EDATE(TODAY(),-24);
"3 Y E A R S",EDATE(TODAY(),-36)},
2,FALSE),0),"yyyy-mm-dd")&"'
group by B
order by
count(B) DESC,
B asc
label count(B) ''")
Using an array
=QUERY(
{A3:A,E3:E},
"Select Col2, count(Col2)
where
Col2 matches '^(?!(?:ITEMS|ExcludeB|ExcludeC)$).+' and
Col1 >= date '"&
TEXT(
IFERROR(
VLOOKUP(
G2,
{"2 4 H O U R S",TODAY()-1;
"3 D A Y S",TODAY()-3;
"7 D A Y S",TODAY()-7;
"2 W E E K S",TODAY()-14;
"1 M O N T H",EDATE(TODAY(),-1);
"3 M O N T H S",EDATE(TODAY(),-3);
"6 M O N T H S",EDATE(TODAY(),-6);
"1 Y E A R",EDATE(TODAY(),-12);
"2 Y E A R S",EDATE(TODAY(),-24);
"3 Y E A R S",EDATE(TODAY(),-36)},
2,FALSE),0),"yyyy-mm-dd")&"'
group by Col2
order by
count(Col2) DESC,
Col2 asc
label
Col2 '',
count(Col2) ''")
I try to remove 1 whitespace from this string:
m y r e a l n a m e i s d o n a l d d u c k
Expected result:
my real name is donald duck
My code are:
def solve_cipher(input)
input.split('').map { |c| (c.ord - 3).chr }.join(' ') # <- Here I tried everything
end
puts solve_cipher('p| uhdo qdph lv grqdog gxfn')
# => m y r e a l n a m e i s d o n a l d d u c k
I tried everything to solve my problem, example:
input.....join(' ').squeeze(" ").strip # => m y r e a l n a m e...
or
input.....join.gsub(' ','') # => myrealnameisdonaldduck
or
input.....join(' ').lstrip # => m y r e a l n a m e...
and so on...
Well, you could split the string into words first, then split each word into characters. Using the same method you used in your code, it could look like this.
def solve_cipher(input) input.split(' ').map{ |w| w.split('').map { |c| (c.ord - 3).chr}.join('')}.join(' ') end
When joining the characters in the same word, we put no space between them; when joining the words together we put one space between them.
As stated in the question, you are using Rails, so you can also try squish method:
def solve_cipher( input )
input.split(' ').map(&:squish).join(' ')
end
str = "m y r e a l n a m e i s d o n a l d d u c k"
str.gsub(/\s(?!\s)/,'')
#=> "my real name is donald duck"
The regex matches a whitespace character not followed by another whitespace character and replaces the matched characters with empty strings. (?!\s) is a negative lookahead that matches a whitespace.
If more than two spaces may be present between words, first replace three or more spaces with two spaces, as follows.
str = "m y r e a l n a m e i s d o n a l d d u c k"
str.gsub(/\s{3,}/, " ").gsub(/\s(?!\s)/,'')
#=> "my real name is donald duck"
I know that it is not a fancy way of doing it but you could just try to create a new string and have a function traversal(input) with a counter initiated at 0, that would return this new string.
It would go through your input (which is here your string) and if the counter is 0 and it sees a space it just ignores it, increments a counter and go to the next character of the string.
If the counter is different of 0 and it sees a space it just concatenates it to the new string.
And if the counter is different of 0 and it sees something different of a space, it concatenates it to the new string and counter equals 0 again.
The trick is to use a capture group
"m y r e a l n a m e i s d o n a l d d u c k".gsub(/(\s)(.)/, '\2')
=> "my real name is donald duck
My website is using Drupal. On first page I have a list of entries and each entry has own URL.
In some cases, I can't understand when, my links looks like:
/node/1%2C157
Instead of:
/node/1157
In my view I found that my content is displayed by this line:
<?php print render($page['content']); ?>
I need to understand where my content is generated and to fix this problem.
Or maybe I can fix this problem from admin panel ?
( I'm using Pathauto and Path modules to rewrite URL's, on other content is working well but in some nodes I have this problem. I tried to regenerate, to remove and generate again but for this nodes nothing happened ).
The %2C occurs in the url because of comma.Please see if you are creating any url with comma in it.This is possibly the cause of such url.
Here is the list of url encoded characters.
URL Encoded Characters
backspace %08
tab %09
linefeed %0A
creturn %0D
space %20
! %21
" %22
# %23
$ %24
% %25
& %26
' %27
( %28
) %29
* %2A
+ %2B
, %2C
- %2D
. %2E
/ %2F
0 %30
1 %31
2 %32
3 %33
4 %34
5 %35
6 %36
7 %37
8 %38
9 %39
: %3A
; %3B
< %3C
= %3D
> %3E
? %3F
# %40
A %41
B %42
C %43
D %44
E %45
F %46
G %47
H %48
I %49
J %4A
K %4B
L %4C
M %4D
N %4E
O %4F
P %50
Q %51
R %52
S %53
T %54
U %55
V %56
W %57
X %58
Y %59
Z %5A
[ %5B
\ %5C
] %5D
^ %5E
_ %5F
` %60
a %61
b %62
c %63
d %64
e %65
f %66
g %67
h %68
i %69
j %6A
k %6B
l %6C
m %6D
n %6E
o %6F
p %70
q %71
r %72
s %73
t %74
u %75
v %76
w %77
x %78
y %79
z %7A
{ %7B
| %7C
} %7D
~ %7E
¢ %A2
£ %A3
¥ %A5
| %A6
§ %A7
« %AB
¬ %AC
¯ %AD
º %B0
± %B1
ª %B2
, %B4
µ %B5
» %BB
¼ %BC
½ %BD
¿ %BF
À %C0
Á %C1
 %C2
à %C3
Ä %C4
Å %C5
Æ %C6
Ç %C7
È %C8
É %C9
Ê %CA
Ë %CB
Ì %CC
Í %CD
Î %CE
Ï %CF
Ð %D0
Ñ %D1
Ò %D2
Ó %D3
Ô %D4
Õ %D5
Ö %D6
Ø %D8
Ù %D9
Ú %DA
Û %DB
Ü %DC
Ý %DD
Þ %DE
ß %DF
à %E0
á %E1
â %E2
ã %E3
ä %E4
å %E5
æ %E6
ç %E7
è %E8
é %E9
ê %EA
ë %EB
ì %EC
í %ED
î %EE
ï %EF
ð %F0
ñ %F1
ò %F2
ó %F3
ô %F4
õ %F5
ö %F6
÷ %F7
ø %F8
ù %F9
ú %FA
û %FB
ü %FC
ý %FD
þ %FE
ÿ %FF