Python Forum
Apply function on different columns as defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Apply function on different columns as defined
#1
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.
Reply
#2
try:

data['BMI'] = data[['Weight', 'Height In Meters']].rename(columns = ['Weight', 'Height']).apply(BMI, axis = 1)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply
#4
Do you have two things named BMI? That error makes it look like you named a list the same thing as your function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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?
Reply
#6
Try renaming your function. At the time that line executes, BMI is a list. Maybe that's getting done in the pandas code somewhere.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  apply(pd.Series) until no more array mikisDeWitte 1 2,739 Apr-17-2021, 08:45 PM
Last Post: Caprone
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,572 Jan-27-2021, 10:59 AM
Last Post: epsilon
  A function to return only certain columns with certain string illmattic 2 2,170 Jul-24-2020, 12:57 PM
Last Post: illmattic
  Apply rolling window function over time dimension of 3D data Staph 0 2,160 Jan-01-2020, 08:31 AM
Last Post: Staph
  Filter value from DataFrame apply a function and save to xlsx zinho 1 2,007 Dec-22-2019, 03:54 PM
Last Post: zinho
  how to apply user defined function to Pandas DataFrame evelynow 3 7,552 Aug-20-2019, 11:35 PM
Last Post: scidam
  Apply Method Question smw10c 4 5,492 Apr-08-2017, 12:47 PM
Last Post: smw10c

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020