Python Forum

Full Version: Apply function on different columns as defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a function that returns the BMI from a given dataframe with columns 'Weight' and 'Height'
Here is the function:
def BMI(data):
    return data['Weight']/(data['Height']**2)
Now, i added new column 'Height In Meters' to the dataframe 'data' with:
data['Height In Meters']=data['Height']/100
What i would like to do next, is to apply the original function on the dataframe 'data',
but instead of using the column 'Height', the calculation would be by using the new column 'Height In Meters'.
the result should be a new column called 'BMI' in the dataframe 'data', that shows for each row the calculation using 'Height In Meters'.

I tried:
data['BMI']=data[['Weight','Height In Meters']].apply(BMI,axis=1)
But that doesn't seem to work.
try:

data['BMI'] = data[['Weight', 'Height In Meters']].rename(columns = ['Weight', 'Height']).apply(BMI, axis = 1)
(Jun-02-2019, 10:30 AM)ichabod801 Wrote: [ -> ]try:

data['BMI'] = data[['Weight', 'Height In Meters']].rename(columns = ['Weight', 'Height']).apply(BMI, axis = 1)

Thank you for trying to help.
I still get an unclear error message when running the code:
Error:
Traceback (most recent call last): File "<input>", line 1, in <module> File "D:\Python Projects\venv\lib\site-packages\pandas\util\_decorators.py", line 197, in wrapper return func(*args, **kwargs) File "D:\Python Projects\venv\lib\site-packages\pandas\core\frame.py", line 4025, in rename return super(DataFrame, self).rename(**kwargs) File "D:\Python Projects\venv\lib\site-packages\pandas\core\generic.py", line 1091, in rename level=level) File "D:\Python Projects\venv\lib\site-packages\pandas\core\internals\managers.py", line 171, in rename_axis obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level)) File "D:\Python Projects\venv\lib\site-packages\pandas\core\internals\managers.py", line 2004, in _transform_index items = [func(x) for x in index] File "D:\Python Projects\venv\lib\site-packages\pandas\core\internals\managers.py", line 2004, in <listcomp> items = [func(x) for x in index] TypeError: 'list' object is not callable
Do you have two things named BMI? That error makes it look like you named a list the same thing as your function.
(Jun-02-2019, 10:05 PM)ichabod801 Wrote: [ -> ]Do you have two things named BMI? That error makes it look like you named a list the same thing as your function.

My function is called BMI,
and the column i would like to add to the dataframe also called 'BMI'.
Any other suggestions?
Try renaming your function. At the time that line executes, BMI is a list. Maybe that's getting done in the pandas code somewhere.
Hi

I am also facing the same issue.

I have a dataframe named with columns: label,text

I defined function as below:
def split_tokens(text):
    message = text.str.lower()
    word_tokens = word_tokenize(message)
    return word_tokens
I am applying this function on a new dataframe column as below
imdb['tokenized_message'] = imdb[['text']].apply(split_tokens,axis=1)
But when I execute this function I get error with message:
Quote:TypeError: ('expected string or bytes-like object', 'occurred at index 0')

Can anyone please help.

Thanks