Skip to content

Instantly share code, notes, and snippets.

@muthugit
Last active March 4, 2019 09:36
Show Gist options
  • Select an option

  • Save muthugit/0549a7f806e0cb05e151509e3aa57a59 to your computer and use it in GitHub Desktop.

Select an option

Save muthugit/0549a7f806e0cb05e151509e3aa57a59 to your computer and use it in GitHub Desktop.
__author__ = "MUTHUPANDIAN"
__email__ = "contact@muthupandian.in"
'''
PERFORM SOME OPERATIONS IN THE EXISTING DATAFRAME USING APPLY METHOD
''''
import pandas as pd
#FUNCTIONS TO PERFORM OPERATION BETWEEN TWO COLUMNS
def sum(cols):
return cols[0]+cols[1]
def avg(cols):
return (cols[0]+cols[1])/2
def mul(cols):
return cols[0]*cols[1]
#CREATE NEW DUMMY DATAFRAME
df = pd.DataFrame({'a':[10,20,30],'b':[33,44,55]})
print(df)
#APPENDING THE COLUMNS TO THE EXISTING DATAFRAME WITH THE OUTPUT FROM FUNCTIONS
df['total']=(df.apply(sum,axis=1))
df['average']=(df.apply(avg,axis=1))
df['multiplication']=(df.apply(mul,axis=1))
#PRINTING DATAFRAME AFTER ADDING THE NEW COLUMNS
print(df)
@muthugit
Copy link
Copy Markdown
Author

muthugit commented Mar 4, 2019

new commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment