Converting R code into python code

Converting R code into python code

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)

Converting R code into python code

You would like to use: datar (I am the author)

from datar.all import *

tmp = test >> 
      group_by(f.InvoiceDocNumber) >> 
      summarise(invoiceprob=max(f.itemprob)) >> 
      mutate(invoicerank=rank(desc(f.invoiceprob)))

Leave a Reply

Your email address will not be published. Required fields are marked *