Question

To run a function on every value in a column of data in a Pandas dataframe....

To run a function on every value in a column of data in a Pandas dataframe. what pandas dataframe method do we use?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts please comment below. Please give upvote if you like this.

Answer:

Python's pandas library provides an member function in Dataframe class to apply a function along the axis of the Dataframe that is along each row or column.

pandas.apply(function , axis=0)

For more understanding example code is below.

Python Code:

# importing pandas
import pandas as pd

# Initialise the matrix with 3 columns val1,val2,val3
matrix = [(1,2,3),(4,5,6),(7,8,9),(10,11,12),(13,14,15),(16,17,18)]
df = pd.DataFrame(matrix, columns=["val1","val2","val3"])
# Print the taking dataframe
print("Contents of the dataframe in object df are ,")
print(df)

# Here lambda is the function to add 10 for every element in columns
modify = df.apply(lambda x:x+10,axis = 0)
# Printing the modified val1 column in dataframe
print("\nContents of the dataframe in object modify are,")
print(modify)

Add a comment
Know the answer?
Add Answer to:
To run a function on every value in a column of data in a Pandas dataframe....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT