Quantcast
Channel: How to apply a function to two columns of Pandas dataframe - Stack Overflow
Browsing latest articles
Browse All 17 View Live

Answer by cottontail for How to apply a function to two columns of Pandas...

Vectorized solutionIn most cases, a given data manipulation task can be performed using the vectorized methods built into numpy and pandas and which are generally faster than an explicit loop or...

View Article



Answer by Rahul for How to apply a function to two columns of Pandas dataframe

It can be done in two simple ways:Let's say, we want sum of col1 and col2 in output column named col_sumMethod 1f = lambda x : x.col1 + x.col2df['col_sum'] = df.apply(f, axis=1)Method 2def f(x):...

View Article

Answer by Rivers for How to apply a function to two columns of Pandas dataframe

Here is a faster solution:def func_1(a,b): return a + bdf["C"] = func_1(df["A"].to_numpy(),df["B"].to_numpy())This is 380 times faster than df.apply(f, axis=1) from @Aman and 310 times faster than...

View Article

Answer by Janosh for How to apply a function to two columns of Pandas dataframe

Another option is df.itertuples() (generally faster and recommended over df.iterrows() by docs and user testing):import pandas as pddf = pd.DataFrame([range(4) for _ in range(4)],...

View Article

Answer by durjoy for How to apply a function to two columns of Pandas dataframe

If you have a huge data-set, then you can use an easy but faster(execution time) way of doing this using swifter: import pandas as pdimport swifterdef fnc(m,x,c): return m*x+cdf = pd.DataFrame({"m":...

View Article


Answer by ajrwhite for How to apply a function to two columns of Pandas...

There is a clean, one-line way of doing this in Pandas:df['col_3'] = df.apply(lambda x: f(x.col_1, x.col_2), axis=1)This allows f to be a user-defined function with multiple input values, and uses...

View Article

Answer by allenyllee for How to apply a function to two columns of Pandas...

I suppose you don't want to change get_sublist function, and just want to use DataFrame's apply method to do the job. To get the result you want, I've wrote two help functions: get_sublist_list and...

View Article

Answer by Ted Petrou for How to apply a function to two columns of Pandas...

Returning a list from apply is a dangerous operation as the resulting object is not guaranteed to be either a Series or a DataFrame. And exceptions might be raised in certain cases. Let's walk through...

View Article


Answer by Qing Liu for How to apply a function to two columns of Pandas...

My example to your questions:def get_sublist(row, col1, col2): return mylist[row[col1]:row[col2]+1]df.apply(get_sublist, axis=1, col1='col_1', col2='col_2')

View Article


Answer by sjm for How to apply a function to two columns of Pandas dataframe

A simple solution is:df['col_3'] = df[['col_1','col_2']].apply(lambda x: f(*x), axis=1)

View Article

Answer by Thomas for How to apply a function to two columns of Pandas dataframe

I'm sure this isn't as fast as the solutions using Pandas or Numpy operations, but if you don't want to rewrite your function you can use map. Using the original example data -import pandas as pddf =...

View Article

Answer by Trae Wallace for How to apply a function to two columns of Pandas...

I'm going to put in a vote for np.vectorize. It allows you to just shoot over x number of columns and not deal with the dataframe in the function, so it's great for functions you don't control or doing...

View Article

Answer by user4284784 for How to apply a function to two columns of Pandas...

A interesting question! my answer as below:import pandas as pddef sublst(row): return lst[row['J1']:row['J2']]df = pd.DataFrame({'ID':['1','2','3'], 'J1': [0,2,3], 'J2':[1,4,5]})print dflst =...

View Article


Answer by JoeCondron for How to apply a function to two columns of Pandas...

The method you are looking for is Series.combine. However, it seems some care has to be taken around datatypes. In your example, you would (as I did when testing the answer) naively call df['col_3'] =...

View Article

Answer by Nitin for How to apply a function to two columns of Pandas dataframe

The way you have written f it needs two inputs. If you look at the error message it says you are not providing two inputs to f, just one. The error message is correct.The mismatch is because...

View Article


Answer by Aman for How to apply a function to two columns of Pandas dataframe

Here's an example using apply on the dataframe, which I am calling with axis = 1. Note the difference is that instead of trying to pass two values to the function f, rewrite the function to accept a...

View Article

How to apply a function to two columns of Pandas dataframe

Suppose I have a function and a dataframe defined as below:def get_sublist(sta, end): return mylist[sta:end+1]df = pd.DataFrame({'ID':['1','2','3'], 'col_1': [0,2,3], 'col_2':[1,4,5]})mylist =...

View Article

Browsing latest articles
Browse All 17 View Live




Latest Images