ScientiaEtVeritas
ScientiaEtVeritas

Reputation: 5278

Apply Custom Classes / Functions on Pandas DataFrames

I have a class DataImporter with a method called getData that I want to apply on a pandas dataframe. The problem: the class / method can just handle single elements.

Imagine I have a DataFrame with three columns id, a and b.

What I actually want to do is something like: (pseudo code)

df["c"] = Class(df["id"]).getData(df["a"], df["b"])

I found out there is something like pandas.Series.apply, but I don't see that it works for the getData part.

Upvotes: 1

Views: 1319

Answers (1)

jezrael
jezrael

Reputation: 862406

I think you need apply with axis=1 for process data by scalars in columns:

df["c"] = df.apply(lambda x: Class(x["id"]).getData(x["a"], x["b"]), axis=1)

Upvotes: 2

Related Questions