I've found a Python module which may be able to help converting Python to R code http://www.xavierdupre.fr/app/pyensae/helpsphinx/notebooks/r2python.html
And interfacing Python to R https://rstudio.github.io/reticulate/
I thought you might want to look at the difference between Python and R, https://shiring.github.io/r_vs_python/2017/01/22/R_vs_Py_post#:~:text=R%20and%20Python%20are%20both,while%20Python%20is%20more%20generalist.
I believe it's best your spend time learning R script https://r4ds.had.co.nz/introduction.html
Answer from waheed rahman on Stack OverflowConverting R code to Python
Coverting R script to python - Stack Overflow
Convert R code into Python code using rpy2 - Stack Overflow
Convert Python to R - Stack Overflow
How do I convert R to Python?
Is the R to Python converter free, and do I need to install anything?
Can I convert Python back to R?
Is this wishful thinking? Or is there anything out there (e.g. maybe ai based) that can take R code and translate it into Python code?
rpy(2) does not convert code. It only allows you an interface to communicate with R from python and issue R commands from within python.
Given that R is very dependent upon statistical libraries that aren't available in python the conversion would generally be a tremendous amount of work. To convert a single line like
lmer (y ~ x1 + x1 + (1|id), mydat)
to something in python would take a great deal of work. If you just want to run that code from python use rpy. Going the other way, which seems to be more what you are interested in, the conversion could often be a bit more straightforward. However, the code would tend to be very poorly optimized for R and run very badly. I recently optimized some R code converted from python by someone who wrote the R code as a more or less direct translation line by line. The optimization came out 1/3 the length and between 20 and 1000 times faster (depending on the function).
John is correct. These are not converting between languages, but interfacing languages. Language conversion is not particularly common, though interfacing them is. However, object conversion is quite common, as that is an important part of interfacing languages.
In addition to Rpy and Rpy2, take a look at RSPython. It also provides for object conversion, and inspired Rpy.
You can use assign to get it all in one chain:
(
test.groupby("InvoiceDocNumber", as_index=False)
.itemprob.max()
.rename(columns={"itemprob":"invoiceprob"})
.assign(invoicerank = lambda x: x.invoiceprob.rank(ascending=False))
)
Output:
InvoiceDocNumber invoiceprob invoicerank
0 0 0.924193 5.0
1 1 0.974173 4.0
2 2 0.978962 3.0
3 3 0.992663 2.0
4 4 0.994243 1.0
Data:
import numpy as np
import pandas as pd
n = 100
test = pd.DataFrame({"InvoiceDocNumber": np.random.choice(np.arange(5), size=n),
"itemprob": np.random.uniform(size=n)})
I got the answer
ddd = test.groupby('InvoiceDocNumber', as_index=False).agg({"itemprob": "max"})
ddd= ddd.rename(columns={'itemprob': 'invoiceprob'})
ddd['invoicerank'] =ddd['invoiceprob'].rank(ascending=0)