How to subtract rows of one pandas data frame from another?

后端 未结 4 1179
执笔经年
执笔经年 2020-12-10 11:26

The operation that I want to do is similar to merger. For example, with the inner merger we get a data frame that contains rows that are present in the first AN

4条回答
  •  不思量自难忘°
    2020-12-10 12:13

    I suggest using parameter 'indicator' in merge. Also if 'on' is None this defaults to the intersection of the columns in both DataFrames.

    new = df1.merge(df2,how='left', indicator=True) # adds a new column '_merge'
    new = new[(new['_merge']=='left_only')].copy() #rows only in df1 and not df2
    new = new.drop(columns='_merge').copy()
    
        Team    Year    foo
    0   Hawks   2001    5
    1   Hawks   2004    4
    2   Nets    1987    3
    4   Nets    2001    8
    5   Nets    2000    10
    

    Reference: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html

    indicator : boolean or string, default False
    
    If True, adds a column to output DataFrame called “_merge” with information on the source of each row. 
    Information column is Categorical-type and takes on a value of 
    “left_only” for observations whose merge key only appears in ‘left’ DataFrame,
    “right_only” for observations whose merge key only appears in ‘right’ DataFrame, 
    and “both” if the observation’s merge key is found in both.
    

提交回复
热议问题