0

I'd like to create a new column in pandas using the .apply method where I can pass a custom function

df["newcol"] = df["oldcol"].apply(lambda x: x + 1)
or
df["newcol"] = df.apply(lambda row: row["oldcol"]+1)

How to achieve this in hy ?

so far I am able to do

(setv (get df "newcol") (. (get d "oldcol") apply)

which just sets newcol to oldcol, but couldn't figure out how to define a function to apply.

dedupe
  • 9
  • 2

1 Answers1

0

I recommend reading through the tutorial, which covers topics such as calling methods and defining anonymous functions. A literal Hy translation of

df["newcol"] = df.apply(lambda row: row["oldcol"]+1)

is

(setv (get df "newcol") (.apply df (fn [row] (+ (get row "oldcol") 1))))
Kodiologist
  • 2,984
  • 18
  • 33
  • Thank you. I have read the tutorial, I was confused this with the way iloc works. Your answers should be included in the tutorial. – dedupe Nov 09 '20 at 00:43