Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python dataframe calculate difference between columns

import pandas as pd
  
# Create a DataFrame
df1 = { 'Name':['George','Andrea','micheal',
                'maggie','Ravi','Xien','Jalpa'],
        'score1':[62,47,55,74,32,77,86],
        'score2':[45,78,44,89,66,49,72]}
  
df1 = pd.DataFrame(df1,columns= ['Name','score1','score2'])
  
print("Given Dataframe :
", df1)
  
# getting Difference
df1['Score_diff'] = df1['score1'] - df1['score2']
print("
Difference of score1 and score2 :
", df1)
Comment

pandas difference between two dataframes

source_df.merge(target_df,how='left',indicator=True).loc[lambda x:x['_merged']!='both']
Comment

find difference between two pandas dataframes

# by doing outer, you will get records from both the sides.
f = df1.merge(df2,indicator = True, how='outer').loc[lambda x : x['_merge']!='both']
Out[421]: 
   A  B     _merge
1  2  3  left_only
2  3  4  left_only
3  3  4  left_only

left_unique_result = f.loc[lambda x: x['_merge'] == 'left_only']
right_unique_result = f.loc[lambda x: x['_merge'] == 'right_only']
Comment

difference between 2 dataframes

df1.merge(df2,indicator = True, how='left').loc[lambda x : x['_merge']!='both']
Out[421]: 
   A  B     _merge
1  2  3  left_only
2  3  4  left_only
3  3  4  left_only
Comment

PREVIOUS NEXT
Code Example
Python :: python slice list 
Python :: python for unity 
Python :: how to check if python is installed on mac 
Python :: float64 python 
Python :: jupyter notebook cell background color 
Python :: string to list python 
Python :: pandas df.to_csv() accent in columns name 
Python :: remove item from list 
Python :: WARNING: This is a development server 
Python :: any python 
Python :: function to remove punctuation in python 
Python :: round to nearest multiple of 5 python 
Python :: pandas create average per group 
Python :: strip characters from a string python 
Python :: multiple input to list 
Python :: python pyaudio error 
Python :: how to print in python 
Python :: list of lists to table python 
Python :: django save object in view 
Python :: python qr decomposition 
Python :: python timestamp to string 
Python :: snapchat api in python 
Python :: read xml file in python 
Python :: catch exception python unittest 
Python :: django queryset multiple filters 
Python :: how to concatenate in python 
Python :: pop element from heap python 
Python :: django check if get parameter exists 
Python :: selenium python tkinter 
Python :: add list python 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =