Last active
March 4, 2019 09:36
-
-
Save muthugit/0549a7f806e0cb05e151509e3aa57a59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = "MUTHUPANDIAN" | |
__email__ = "[email protected]" | |
''' | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
new commit