I have a custom function that generates a new column based on an equation, but I cannot see col3
after running the function and printing out the dataframe.
How can I get the new column to save as a pandas dataframe?
Here is what I have done thus far:
#Import modules
import pandas as pd
import numpy as np
#Define the custom function
def example(a, b, c):
"""
Example function, generates a new column based on user inputs
"""
#Generate input dataframe
d = {
'col1': [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
'col2': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
}
dataframe = pd.DataFrame(data=d)
#Create a new column based on an equation
dataframe['col3'] = dataframe['col1'] * a * b * c
return dataframe
Calling the function...
example(2, 4, 6)
...gives this output:
col1 col2 col3
0 1 1 48
1 1 2 48
2 1 3 48
3 1 4 48
4 1 5 48
5 1 6 48
6 1 7 48
7 1 8 48
8 1 9 48
9 1 10 48
10 1 11 48
11 1 12 48
12 1 13 48
13 1 14 48
14 1 15 48
15 1 16 48
16 1 17 48
17 1 18 48
18 1 19 48
19 1 20 48
However, printing the dataframe shows that the column hasn't been added:
dataframe
An error message is generated:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 dataframe
NameError: name 'dataframe' is not defined
How can I fix this so that col3
is appended onto the original dataframe?
CodePudding user response:
You need to save it like this. Your functions works perfectly fine:
df = example(2, 4, 6)