question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A map method to work with many columns

See original GitHub issue

Hi,

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:open
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
TomAugspurgercommented, Apr 24, 2018

Thanks for the clarification. Perhaps you could update your original post with your new proposal.

Since map does not exist for DataFrame, you are not going to break anything.

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.

About your example, apply would detect that f expect 1 argument and there it would behave as it does now.

In general, we try to avoid magic behavior like this. It often leads to surprising / difficult to debug behavior in unanticipated cases.

1reaction
oricoucommented, Apr 24, 2018

I want to apply a 2 arguments function to 2 columns of my DataFrame. As you said apply expect 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 map instead of apply to 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:


df = pd.DataFrame({'A':[1,2], 'B':[3,4]})
df.apply(lambda x,y: x+2*y, axis=1)   # does not work

# what about having?
df.map(lambda x,y: x+2*y)

Since map does not exist for DataFrame, you are not going to break anything.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found