我有两个数据帧。
pandas.core.frame.DataFrame
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")
目标:
使用main_df
过滤或删除exclude_df
的行,并得到以下结果。
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
我的代码:
# 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)
是否存在另一种无需将pandas.core.series.Series
转换为pandas.core.frame.DataFrame
的方法?
答案 0 :(得分:3)
使用:
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