简体   繁体   中英

Finding the difference between two data frames

I have two data frames say df1, df2 each has two columns ['Name', 'Marks']

I want to find the difference between the two ifs for corresponding Name Values.

Eg:

df = pd.DataFrame([["Shivi",70],["Alex",40]],columns=['Names', 'Value'])
df2 = pd.DataFrame([["Shivi",40],["Andrew",40]],columns=['Names', 'Value'])

For df1-df2 I want

pd.DataFrame([["Shivi",30],["Alex",40],["Andrew",40]],columns=['Names', 'Value'])

You can use:

diff = df1.set_index("Name").subtract(df2.set_index("Name"), fill_value=0)

So a complete program will look like this:

import pandas as pd

data1 = {'Name': ["Ashley", "Tom"], 'Marks': [40, 50]}
data2 = {'Name': ["Ashley", "Stan"], 'Marks': [80, 90]}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

diff = df1.set_index("Name").subtract(df2.set_index("Name"), fill_value=0)

print(diff)

Output:

        Marks
Name
Ashley  -40.0
Stan    -90.0
Tom      50.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM