pandas中左连接中的不匹配左表记录

2024-09-28 05:18:00 发布

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

学生有两个数据帧。“费用”数据框中缺少部分学生的费用明细。我想返回所有学生的详细资料,谁的费用细节丢失。“Class”、“Section”和“RollNo”三个字段构成了一个唯一的组合。在

Students = pd.DataFrame({
    'Class': [7, 7, 8],
    'Section': ['A', 'B', 'B'],
    'RollNo': [2, 3, 4],
    'Student': ['Ram', 'Rahim', 'Robert']
})

Fee = pd.DataFrame({
    'Class': [7, 7, 8],
    'Section': ['A', 'B', 'B'],
    'RollNo': [2, 2, 3],
    'Fee': [10, 20, 30]
})

^{pr2}$
Fee

   Class  Fee  RollNo Section
0      7   10       2       A
1      7   20       2       B
2      8   30       3       B

基本上,我希望在基于上述3个字段的“Students”和“Fee”数据帧之间执行左联接时,从左表中查找不匹配的记录。使用Python中的Pandas实现这一点的最简单方法是什么?在

非常感谢!在


Tags: 数据dataframesectionstudent学生细节classram
2条回答

如果在Fee数据帧的Fee列中没有NaN,请使用^{}anf filter by ^{}和{a3}:

df = pd.merge(Students, Fee, how='left')
print (df)
   Class  RollNo Section Student   Fee
0      7       2       A     Ram  10.0
1      7       3       B   Rahim   NaN
2      8       4       B  Robert   NaN

df1 = df[df['Fee'].isna()].drop('Fee', axis=1)
#for oldier versions of pandas
#df1 = df[df['Fee'].isnull()].drop('Fee', axis=1)
print (df1)
   Class  RollNo Section Student
1      7       3       B   Rahim
2      8       4       B  Robert

使用NaNs的更一般的解决方案还将参数indicator添加到merge并使用left_only过滤行:

^{pr2}$

我对这个概念很感兴趣。在

选项1

  1. pandas.concatkeys参数一起使用
  2. 确保Studentss部分的结果MultiIndex的第一个级别的值为'stu'。在
  3. pandas.DataFrame.drop_duplicates与参数keep=False一起使用可删除所有重复。在
  4. 通过使用loc,将注意力集中在{}部分。在

catted = pd.concat([Students, Fee], keys=['stu', 'fee'])
dropped = catted.drop_duplicates(['Class', 'RollNo', 'Section'], keep=False)
index = dropped.loc['stu'].index

Students.loc[index]

   Class  RollNo Section Student
1      7       3       B   Rahim
2      8       4       B  Robert

方案2

使用元组列表上的集合,取一个差异并与一个人工数据帧合并。在

^{pr2}$

相关问题 更多 >

    热门问题