What is the Python equivalent to R data.table column creation?

Question:

I’m in the process of converting my R scripts to python. Is there a similar process in creating new columns that r data.table does in the J step? Below is my example code in R:

dat[,Returned_on_time:= ifelse(Contract_End_Date==Estimated_In_Date,'Yes','No')]

Thanks

Asked By: Zachqwerty

||

Answers:

Thanks to @furas for the fast solution using numpy.

df["Returned_on_time"] = np.where(df["Contract_End_Date"] == df["Estimated_In_Date"], 'Yes', 'No')
Answered By: Zachqwerty

Your R example illustrates data.table‘s fast in-place generation of your new column.

A python equivalent of in-place creation in the j slot is shown below using python datatable

from datatable import dt,f,update

dat=dt.Frame({"Contract_End_Date":["2022-01-02", "2022-04-15", "2022-12-19"],
              "Estimated_In_Date":["2022-04-26", "2022-04-15", "2022-11-30"]
             })

dat[:, dt.update(Returned_on_time = dt.ifelse(f.Contract_End_Date == f.Estimated_In_Date, "Yes", "No"))]

Output:

   | Contract_End_Date  Estimated_In_Date  Returned_on_time
   | str32              str32              str32           
-- + -----------------  -----------------  ----------------
 0 | 2022-01-02         2022-04-26         No              
 1 | 2022-04-15         2022-04-15         Yes             
 2 | 2022-12-19         2022-11-30         No              
[3 rows x 3 columns]
Answered By: langtang
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.