I have some functions that act on a list and return a list. I would like to create a column on a Pandas Dataframe such that the new column is the list returned by one of the functions acting on some other column of the dataframe.
In python-like pseudocode:
def function(parameter, list):
...
return output_list
df['New Column'] = function(parameter, df['Old Column'])
I have tried different options including something like the code above, using .apply() method and others... with no success.
Is there a way to do this? Thank you!
EDIT: See Brian Pendleton's answer for the solution. Columns in a dataframe are pandas' Series objects. Just have to create a Series out of the desired list.
df['New_Column'] = pd.Series(data=function(parameter,list))
If you're sending df["old column"]
to the function, then you're sending a pandas.Series
object. Why not just operate with that series and return a new series of the same shape. Then you can just use your assignment to new column as you have it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments