从多个数据帧中提取唯一行

2024-09-27 04:23:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建多个只具有唯一值的过滤数据帧

数据集1

Account     Verified     Paid   Col1 Col2 Col3
1234        True        True     ...  ...  ...
1237        False       True    
1234        True        True
4211        True        True
1237        False       True
312         False       False

数据集2

Account          Verified   Paid   Col1 Col2 Col3
41                True      True    ... ... ...
314               False     False
41                True      True
65                False     False

多个数据帧称为dtf[i],其中i从1变为2。 预期产出将是:

过滤1

Account     Verified     Paid
1234        True        False
1237        False       True
4211        True        True

312         False       False

过滤2

Account          Verified   Paid
41                True      True
314               False     False
65                False     False

如何提取这些唯一值


Tags: 数据falsetrueaccountcol2col3col1verified
2条回答

如果要删除重复项,请使用以下代码:pd.DataFrame.drop_duplicates

import pandas as pd
df = pd.DataFrame({'col_1':['A','B','A','B','C'], 'col_2':[3,4,3,5,6], 'col_3':[0,0.1,0.2,0.3,0.4]})
print(df)
df.drop_duplicates(['col_1','col_2'], inplace = True)
print(df)

如果要传递所有列以定义唯一性,请改用df.columns

df.drop_duplicates(df.columns, inplace = True)
print(df)

编辑:

要传递列表中的所有数据帧,并且不想替换df,请使用下面的代码并说inplace = False(默认值)

lst_df = [pd.DataFrame({'col_1':['A','B','A','B','C'], 'col_2':[3,4,3,5,6], 'col_3':[0,0.1,0.2,0.3,0.4]}), pd.DataFrame({'col_1':['A','B','A','B','C'], 'col_2':[3,4,3,5,6], 'col_3':[0,0.1,0.2,0.3,0.4]})]
new_lst_df = []
[new_lst_df.append(lst_df[i].drop_duplicates(['col_1', 'col_2'])) for i in range(len(lst_df))]
print(new_lst_df)

如果只想删除重复的帐号

 dtf[i].drop_duplicates(subset ="Account", 
                     keep = False, inplace = True) 

或者,如果要删除精确的重复行:

 dtf[i].drop_duplicates(subset =[["Account","Verified","Paid"]], 
                     keep = False, inplace = True) 

我希望有帮助

相关问题 更多 >

    热门问题