I have a class inside a .py file that contains two functions.
new_column
generates a new column based on values of existing columns (Col1 and Col2).
transform_df
applies the previous function to a dataframe, via a lambda expression.
The end result looks like this:
Col1 Col2 col3
0 a b ab
1 a b ab
2 a c None
3 a b ab
How do I use these functions in a new file/notebook?
# kept within file df_functions.py
class Functions():
def __init__(self, path):
self.path = path # path to .csv
# function to create new column, based on Col1 and Col2 values
def new_column(self, row):
if (row['Col1'] == 'a') & (row['Col2'] == 'b'):
return 'ab'
# apply previously defined function via lambda expression
def transform_df(self, path):
df = pd.read_csv(self.path)
# apply function 'new_column' to df
df['col3'] = df.apply(lambda row: self.new_column(row), axis=1)
# other potential functions applications here
return df
I have tried the following:
from df_functions import Functions
df_path = '../datafile.csv'
FunctionsObject = Functions(path=df_path)
new_df = FunctionsObject.transform_df(path=df_path)
However this returns
NameError: ("name 'new_column' is not defined", 'occurred at index 0')