-1

I want to recreate a snippet code from R to Python. I have this tibble from R (dataframe in Python) which is somehow look like this:

column1       column2                                 column3
amsterdam     het dag calamiteit bij doen gratis dag  2013
rotterdam     blijdorp groet gratis burp het ik ben   2015

with this code below, I tried to extract description as single character string. This is the code:

#R code
for (i in 1:nrow(tibble)) {
    des <- pull(tibble[i,2])
}

#Python code
for i in df:    
    des = df['column2'].str.split(expand=True).stack()

and then the series of des (we got from df['column']) would look like this in python:

het
dag
calamiteit
bij
doen
gratis
dag
blijdorp
groet
burp
het
ik
ben

But, then I want to recreate this code from R to Python which I don't know how:

if (grepl("^\\s*$", des) == TRUE) { # if description is only whitespace then skip
    trns <- tibble(translatedText = "", detectedSourceLanguage = "", text = "")

especially the grepl function.

What does it equal to in Python? and what's the best Python code to recreate that? thank you

  • Does this answer your question? [simplest python equivalent to R's grepl](https://stackoverflow.com/questions/38745710/simplest-python-equivalent-to-rs-grepl) – Matt Jan 17 '20 at 15:43

2 Answers2

0

A nearly exact equivalent of grepl is re.match. See this small example:

import re
data = ["00het", "dags"]

matches = [re.match(r"\d{2}", str_) for str_ in data]

While the first string has a match, the other string is None since there aren´t two digits in it. I hope this may be a good starting point for you to translate you expression from R to python

0

I got a perfect to recreate the R script from above. This is the Python code:

 if [re.match(r'^\s*$', i) for i in des]:
        trns = i

so if I have a series of strings like this:

root
wit
geel

with
asd

goed
black
red

then after I run it with if statement, then I would get result like this:

[None,
None,
None,
None,
None,
None,
<re.Match object; span=(0, 1), match=' '>,
None,
<re.Match object; span=(0, 0), match=''>,
<re.Match object; span=(0, 1), match=' '>]