A got src block
#+NAME: gdt
#+BEGIN_SRC ruby :var date="14-05-2018"
require 'active_support/all'
table_header_days = []
today = Date.parse(date)
0.upto(7 - today.wday) do |n|
table_header_days.push("#{(today + n.days).day} #{(today + n.days).strftime('%a')}")
end
[['', table_header_days, ''].flatten, nil, ['row 1'], ['row 2'], ['row 3'], ['row 4']]
#+END_SRC
It outputs like table
#+RESULTS: gdt
| | 14 Mon | 15 Tue | 16 Wed | 17 Thu | 18 Fri | 19 Sat | 20 Sun | |
|-------+--------+--------+--------+--------+--------+--------+--------+---|
| row 1 | | | | | | | | |
| row 2 | | | | | | | | |
| row 3 | | | | | | | | |
| row 4 | | | | | | | | |
I want to use this src block in other src block
#+BEGIN_SRC ruby :noweb eval :exports table
<<gdt("14-05-2018")>>
#+END_SRC
I expect to get table but got an error
-:3: syntax error, unexpected tIDENTIFIER, expecting ')'
i" "19 Sat" "20 Sun" "") hline ("row 1") ("row 2") ("row 3")
^
-:3: syntax error, unexpected '(', expecting ')'
"20 Sun" "") hline ("row 1") ("row 2") ("row 3") ("row 4"))
^
-:3: syntax error, unexpected '(', expecting keyword_end
") hline ("row 1") ("row 2") ("row 3") ("row 4"))
^
-:3: syntax error, unexpected ')', expecting keyword_end
("row 1") ("row 2") ("row 3") ("row 4"))
^
-:3: syntax error, unexpected ')', expecting keyword_end
("row 2") ("row 3") ("row 4"))
^
-:6: syntax error, unexpected end-of-input, expecting keyword_end
What I did wrong?
The only thing you can use inside the <<...>> markers in a noweb "call" is a code ID, i.e. the name of the block that is to replace it. In particular, you cannot pass arguments like this <<gdt("arg")>>.
Try changing your second block to this:
#+BEGIN_SRC ruby :noweb eval :exports table :var date="14-05-2018"
<<gdt>>
#+END_SRC
Related
I have a dataset as follows:
| id | text |
--------------
| 01 | hello world |
| 02 | this place is hell |
I also have a list of keywords I'm search for:
Keywords = ['hell', 'horrible', 'sucks']
When using the following solution using .rlike() or .contains(), sentences with either partial and exact matches to the list of words are returned to be true. I would like only exact matches to be returned.
Current code:
KEYWORDS = 'hell|horrible|sucks'
df = (
df
.select(
F.col('id'),
F.col('text'),
F.when(F.col('text').rlike(KEYWORDS), 1).otherwise(0).alias('keyword_found')
)
)
Current output:
| id | text | keyword_found |
-------------------------------
| 01 | hello world | 1 |
| 02 | this place is hell | 1 |
Expected output:
| id | text | keyword_found |
--------------------------------
| 01 | hello world | 0 |
| 02 | this place is hell | 1 |
Try below code, I have just change the Keyword only :
from pyspark.sql.functions import col,when
data = [["01","hello world"],["02","this place is hell"]]
schema =["id","text"]
df2 = spark.createDataFrame(data, schema)
df2.show()
+---+------------------+
| id| text|
+---+------------------+
| 01| hello world|
| 02|this place is hell|
+---+------------------+
KEYWORDS = '(hell|horrible|sucks)$'
df = (
df2
.select(
col('id'),
col('text'),
when(col('text').rlike(KEYWORDS), 1).otherwise(0).alias('keyword_found')
)
)
df.show()
+---+------------------+-------------+
| id| text|keyword_found|
+---+------------------+-------------+
| 01| hello world| 0|
| 02|this place is hell| 1|
+---+------------------+-------------+
Let me know if you need more help on this.
This should work
Keywords = 'hell|horrible|sucks'
df = (df.select(F.col('id'),F.col('text'),F.when(F.col('text').rlike('('+Keywords+')(\s|$)').otherwise(0).alias('keyword_found')))
id
text
keyword_found
01
hello world
0
02
this place is hell
1
How can I replace a string which contains only whitespace with None?
Example strings:
Input:
" ",
" Hello There ",
"Hi World"
Output:
null,
" Hello There ",
"Hi World"
I have used below line but replace everything with null.
df=df.withColumn('TITLE_LINE_3',F.regexp_replace(F.trim(df.TITLE_LINE_3),"^\s+$",None))
You can trim the column and check if it's empty to replace it with null.
df.withColumn('TITLE_LINE_3', when(trim('TITLE_LINE_3') == '', None).otherwise(col('TITLE_LINE_3')))
You can use a an udf to strip the whitespaces from your column and further identify using a when-otherwise , if they are empty and replace them with None
Data Preparation
sparkDF = sql.createDataFrame(
[
(" ",),
(" Hi There ",),
("Hello World",),
],
("text",)
)
sparkDF.show()
+-----------+
| text|
+-----------+
| |
| Hi There |
|Hello World|
+-----------+
Strip UDF
strip_udf = F.udf(lambda x:x.strip(),StringType())
sparkDF = sparkDF.withColumn('preprocessed_text',strip_udf(F.col('text')))
sparkDF = sparkDF.withColumn('preprocessed_text',F.when(F.col('preprocessed_text') == '',None)\
.otherwise(F.col('preprocessed_text'))\
)
sparkDF.show()
+-----------+-----------------+
| text|preprocessed_text|
+-----------+-----------------+
| | null|
| Hi There | Hi There|
|Hello World| Hello World|
+-----------+-----------------+
I have a lisp data (list of lists)
(setf *table-data*
'((1 "Team1"
(("member 1" "data1")
("member 2" "data2")
("member 3" "data3")))
(2 "Team2"
(("member 1" "data1")
("member 2" "data2")
("member 3" "data3")))))
When I try to convert this list into org-table, using (princ *table-data*) I am getting the output,
| 1 | Team1 | ((member 1 data1) (member 2 data2) (member 3 data3)) |
| 2 | Team2 | ((member 1 data1) (member 2 data2) (member 3 data3)) |
org-table is considering only the outer most list elements.
But I would like to get the output as:
| 1 | Team1 | member 1 | data1 |
| | | member 2 | data2 |
| | | member 3 | data3 |
| 2 | Team2 | member 1 | data1 |
| | | member 2 | data2 |
| | | member 3 | data3 |
Is there any possibility to achieve this?
Also, I want to convert back this table into a list of lists instead of plain lists.
[Partial answer only: this answer deals with the translation of a table from a lisp data structure as Org mode does it.]
The lisp representation of the table that you want to produce is as follows:
#+begin_src emacs-lisp
(setq x '((1 "Team1" "member 1" "data1")
("" "" "member 2" "data2")
("" "" "member 3" "data3")
(2 "Team2" "member 1" "data1")
("" "" "member 2" "data2")
("" "" "member 3" "data3")))
x
#+end_src
#+RESULTS:
| 1 | Team1 | member 1 | data1 |
| | | member 2 | data2 |
| | | member 3 | data3 |
| 2 | Team2 | member 1 | data1 |
| | | member 2 | data2 |
| | | member 3 | data3 |
as you can see by evaluating the source block.
What remains to be done is to translate from your desired representation to this representation and vice-versa.
I have just started using org-mode and org-babel as a lab notebook. I am trying to use a code block to fill in two columns of a table. The code block seems to work for the first column because those are the right numbers. However, when trying to concatenate a string to the file name in column three so the code blocks works on a different set of files it seems to just run the code block on the original files instead, which produces the same output as column one.
#+name: tRNAs
#+begin_src sh :var filename="" :results silent
cd Data/tRNA
grep -c ">" $filename
#+end_src
#+tblname: sequences
| # of Sequences before QC | # after QC | Original File name|
|--------------------------+------------+------------------|
| 681865 | 681865 | read1 |
| 324223 | 324223 | read2 |
| 1014578 | 1014578 | read3 |
| 971965 | 971965 | read4 |
| 931777 | 931777 | read5 |
| 810798 | 810798 | read6 |
| 965134 | 965134 | read7 |
| 718474 | 718474 | read8 |
|--------------------------+------------+------------------|
#+TBLFM: $1='(org-sbe tRNAs (filename (concat "\"" $3 "\"")))
#+TBLFM: $2='(org-sbe tRNAs (filename (concat "\"final_" $3 "\"")))
I have a table as follow:
| Xn | S | Pn |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 1 | 0 |
I would like to search through columns Xn and S and return the value of Pn for which Xn=1 and S=0.
Can anyone advise on how I could go about doing this?
#+tblname: example-table
| Xn | S | Pn |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 1 | 0 |
#+source: compute-table
#+begin_src emacs-lisp :var table=example-table
(require 'cl)
(loop for (xn s pn) in (rest table)
when (and (= xn 1) (= s 0)) collect pn)
#+end_src
#+results: compute-table
| 1 |
Use org-babel: Name the table and use it as input for a function that does the search in a language of your choice (out of the many languages supported by org).
In pseudo code:
#+tblname: my_table
|Xn|S|Pn|
| 0|0|9 |
[...]
#+name filter_table
#+begin_src lang :var tbl=my_table :results output
filter tbl # tbl (my_table by default) is passed in as array of arrays (or list of lists)
print matching Pn
#+end_src