我有两个各有5列的数据框。在这两个数据帧中,前4列的值应相同。如何比较两个数据框的前4列是否相等?
df1列['a','b','c','d','count']
df2列['a','b','c','d','count']
答案 0 :(得分:1)
df1.csv
a,b,c,d,count
1,2,3,4,10
1,2,3,4,11
1,2,5,4,12
1,2,3,4,13
df2.csv
a,b,c,d,count
1,2,3,4,20
1,2,3,4,21
1,2,5,4,22
1,2,3,4,23
test.py
#!/bin/python
import pandas as pd
df1 = pd.read_csv('df1.csv')
df2 = pd.read_csv('df2.csv')
print((df1[['a', 'b', 'c', 'd']] == df2[['a', 'b', 'c', 'd']]).all().all())
# => True