1

I have two data frames.

  1. Type of main_df: pandas.core.frame.DataFrame
  2. Type of exclude_df : pandas.core.series.Series
import pandas as pd

main_df = pd.DataFrame(
    {
        "user_name": ["Tommy", "Martin", "Andy", "Lauren", "Will", "Turner", "Henry"],
        "user_id": ["03920", "34233", "02342", "32324", "52323", "20932", "02034"],
        "col_0": [2, 4, 8, 0, 3, 5, 3],
        "col_1": [10, 2, 1, 8, 2, 3, 2],
    }
)

exclude_df = pd.Series(['02342', '52323', '02034'])
exclude_df = exclude_df.rename("user_id")

Goal: Filter or remove rows of main_df using exclude_df and get the below result.

    user_name   user_id  col_0    col_1
0   Tommy       03920       2       10
1   Martin      34233       4       2
2   Lauren      32324       0       8
3   Turner      20932       5       3

My code:

# convert series to dataframe
exclude_df = exclude_df.to_frame()
_df = main_df.merge(exclude_df,on=['user_id'])
result_df = main_df[~main_df.user_id.isin(_df.user_id)]
print(result_df)

Is there another method to achive this without convert pandas.core.series.Series to pandas.core.frame.DataFrame ?

xcen
  • 652
  • 2
  • 6
  • 22

1 Answers1

4

Use:

main_df[~main_df['user_id'].isin(exclude_df)]

  user_name user_id  col_0  col_1
0     Tommy   03920      2     10
1    Martin   34233      4      2
3    Lauren   32324      0      8
5    Turner   20932      5      3
luigigi
  • 4,146
  • 1
  • 13
  • 30