1

I have two dataframes,

df1
Name   | std
kumar  | 8
Ravi   | 10
Sri    | 2
Ram    | 4


df2,
Name   | std
Sri    | 2
Ram    | 4

I want to subtract df2 rows from df1 and I tried,

df1.subtract(df2,fill_value=None)

but I am getting error,

 TypeError: unsupported operand type(s) for -: 'str' and 'str' 

My desired output,

df3
Name   | std
kumar  | 8
Ravi   | 10
Pyd
  • 6,017
  • 18
  • 52
  • 109
  • It is not clear what the column names are in your 'dataframe'. The method `subtract` will align the dataframe on matching indices and columns (even the index names need to be identical). Try to paste the output of `df.iloc[:5, :5]` so that we can get a better idea what your data looks like. – Alexander Aug 02 '17 at 07:26

3 Answers3

9

Use pd.Series.isin

df1[~df1.Name.isin(df2.Name)]

    Name  std
0  kumar    8
1   Ravi   10
piRSquared
  • 285,575
  • 57
  • 475
  • 624
3

You an use merge with parameter indicator and outer join, query for filtering and then remove helper column with drop:

DataFrames are joined on all columns, so on parameter can be omit.

df1 = pd.DataFrame({'Name':['kumar','Ravi','Sri','Ram'],
                    'std':[8,10,2,4],
                    'col':list('abcd')})
print (df1)
    Name col  std
0  kumar   a    8
1   Ravi   b   10
2    Sri   c    2
3    Ram   d    4

df2 = pd.DataFrame({'Name':['Sri','Ram'],
                    'std':[2,4],
                    'col':list('cd')})
print (df2)
  Name col  std
0  Sri   c    2
1  Ram   d    4

df3 = pd.merge(df1, df2, how='outer', indicator=True)
        .query('_merge=="left_only"')
        .drop('_merge',1)
print (df3)
    Name col  std
0  kumar   a    8
1   Ravi   b   10
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

You "numbers" are not numbers but strings. Make them numbers first. If only some columns are numeric, select them before conversion:

numeric = ["c1", "c2", "c3", ...] # All numeric columns
df1.set_index('Name')[numeric].astype(int) \
- df2.set_index('Name')[numeric].astype(int)
DYZ
  • 55,249
  • 10
  • 64
  • 93