pandas dataframe fast apply function on multiple columns

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



pandas dataframe fast apply function on multiple columns



I have a dataframe df with multiple columns (not sure how many). One of the columns is called x. I have defined a function my_function that takes 2 columns as inputs and does something them and returns a new column.
I want to perform the my_function(df[x], df[y]) on all columns of the dataframe (where y is all columns one by one except for x) and return a new df with exact same column names but update values from my_function:


x


my_function


my_function(df[x], df[y])


y


x


my_function


new_df = my_function(df[x], df[y])



where y = all columns in df except for x. What is the fastest way to do this?





For the fastest way, you need to tell us more about my_function. Your calculation may be vectorisable.
– jpp
Aug 13 at 8:47


my_function




2 Answers
2



if your function performs row-wise preprocessing, you can use the method apply (see doc ) for each column:


columns = [item for item in df.columns.values if item!=x]
for c in coulmns:
df[c] = df.apply(lambda row: my_function(row[c], row[x]) ,axis=1)



If not, can you share "my_function"'s body ?



Bests,



You could try with something like this:


x = 'col_name'
[my_function(df[x], df[col]) for col in df if col!=x]



Example:


df = pd.DataFrame( 'a':[1,2],'b':[4,3],'c':[5,9], 'd':[2,9])
x = 'a'
df1 = pd.DataFrame()
def my_function(a,b, name):
c = a+b
df1[name] = c

[my_function(df[x], df[col], name) for name, col in enumerate(df) if col!=x]
print df1



Output:


1 2 3
0 5 6 3
1 5 11 11





Thanks! but is it possible to assign the result to a new_df with the same columns? (each col of df data is processed at my_function and the result of the process will be in new_df) ? I tried the following but doesn't work! [new_df[col]= my_function(df[x], df[col]) for col in df if col!=x]
– sst
Aug 14 at 1:55






@sst I've updated the answer with an example, let me know if it is ok. I've added a 3rd parameter which is the columns name, but you can define the name inside the function if you prefer
– Joe
Aug 14 at 5:56







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS