我正在尝试合并两个数据帧,其中结果数据帧只是列1-8相同的重复行。这些是我的数据框:
check_df 0 1 2 3 4 5 6 7 8
0 1 794 1 192.168.0.1 qrf default SQL Server Windows cilaric98
1 2 2558 1 192.168.0.2 qrf default Oracle Windows cilaric98
2 3 2840 1 192.168.0.3 qrf default Oracle Windows cilaric008
3 4 3290 1 192.168.0.4 qrf default SQL Server Windows cilaric98
4 5 3175 1 192.168.0.5 a2d2 default SQL Server Windows cilaric98
5 6 4189 1 192.168.0.21 a2d2 default SQL Server Windows cilaric98
dataframe 0 1 2 3 4 5 6 7 8
0 1.0 794 1 192.168.0.1 qrf default SQL Server Windows cilaric98
1 2.0 2558 1 192.168.0.2 qrf default Oracle Windows cilaric98
2 3.0 2840 1 192.168.0.3 qrf default Oracle Windows cilaric008
3 4.0 3290 1 192.168.0.4 qrf default SQL Server Windows cilaric98
4 5.0 3175 1 192.168.0.5 a2d2 default SQL Server Windows cilaric98
5 6.0 3915 1 192.168.0.6 a2d2 default SQL Server Windows cilaric98
6 7.0 3886 1 192.168.0.7 a2d2 default SQL Server Windows cilaric98
运行此:
dupes_df = check_df.merge(dataframe, how='inner', on=[1,2,3,4,5,6,7], indicator=True).drop_duplicates(keep='first')
dupes_df = dupes_df[dupes_df['_merge'] == 'both']
返回几乎两倍的列和更多的行,然后返回其中一个数据框包含的内容。我期望的输出是:
check_df 0 1 2 3 4 5 6 7 8
0 1 794 1 192.168.0.1 qrf default SQL Server Windows cilaric98
1 2 2558 1 192.168.0.2 qrf default Oracle Windows cilaric98
2 3 2840 1 192.168.0.3 qrf default Oracle Windows cilaric008
3 4 3290 1 192.168.0.4 qrf default SQL Server Windows cilaric98
4 5 3175 1 192.168.0.5 a2d2 default SQL Server Windows cilaric98
答案 0 :(得分:2)
您可以尝试单线操作:
check_df.merge(dataframe, on=[1,2,3,4,5,6,7], suffixes=('','_y'))[check_df.columns]
输出:
0 1 2 3 4 5 6 \
0 1 794 1 192.168.0.1 qrf default SQL Server
1 2 2558 1 192.168.0.2 qrf default Oracle Windows
2 3 2840 1 192.168.0.3 qrf default Oracle Windows
3 4 3290 1 192.168.0.4 qrf default SQL Server Windows
4 5 3175 1 192.168.0.5 a2d2 default SQL Server Windows
7 8
0 Windows cilaric98
1 cilaric98 None
2 cilaric008 None
3 cilaric98 None
4 cilaric98 None