A map method to work with many columns
See original GitHub issueHi,
I would like to map a function expecting N arguments to a DataFrame of N columns. Today to do that I do:
df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]})
def f(x,y):
return x*y
df[['A', 'B']].apply(lambda x: f(*x), axis=1)
So the map method for DataFrame would be:
def map(self, f):
return self.apply(lambda x: f(*x), axis=1)
if I am right. Then df.map(f) would produce a Serie.
Note that with 1 colummn and a 1 argument function map would work as numpy.map does.
Thanks,
Olivier.
ps: I edited this post after the talk I had with Tom, so it must seem strange now 😃
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
How to map a function using multiple columns in pandas?
A even faster way to map multiple columnns is to use frompyfunc from numpy to create a vectorized version of the python function...
Read more >Transforming Pandas Columns with map and apply
In this tutorial, you'll learn how to transform your Pandas DataFrame columns using vectorized functions and custom functions using the map ...
Read more >A Visual Guide to Pandas map( ) function
The Pandas map( ) function is used to map each value from a Series object to another value using a dictionary/function/Series. It is...
Read more >Pandas map: Change Multiple Column Values with a Dictionary
In this tutorial, we will learn how to use Pandas map() function to replace multiple column values using a dictionary.
Read more >pandas map() Function
You can only use the Series.map() function with the particular column of a pandas DataFrame. If you are not aware, every column in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks for the clarification. Perhaps you could update your original post with your new proposal.
Indeed, this would be preferable. Then we’ll have to decide whether the additional method is worth implementing, & maintaining. Especially since your workaround is relatively straightforward.
In general, we try to avoid magic behavior like this. It often leads to surprising / difficult to debug behavior in unanticipated cases.
I want to apply a 2 arguments function to 2 columns of my DataFrame. As you said
applyexpect a vector (a column or a row) which means the function given to apply should have one argument. Imagine you have functions with N arguments and you want to map it on all the rows for the N columns you have selected. The best way I have found to do that is the use of a lambda function to call my function (as explained in my first post).Now thinking about it, it may be more logic to use
mapinstead ofapplyto do what I want. Map a function expecting N arguments to N columns would run row by row the function with the N values. It would behave the same than map with a 1 argument function applied to a Serie.Example:
Since
mapdoes not exist for DataFrame, you are not going to break anything.