我想读取 2 个 CSV 文件并进行比较。 我需要处理数据,所以我将它们作为字符串读取。 为了比较,我使用 Pandas 查询并输出未处理的行。
当我在行中有一个以 0 开头和以 0 结尾的数据时,尽管它们是完全相同的数据,但它被识别为不匹配。 你知道为什么它被识别为不同的数据吗?
数据
csv_1
id, id_2, name, unit, dep, sales, code, others
1, 22, apple, 100, 243837463, 89.90, 0214008000000, 88899
2, 23, orange, 403, 111839281, 10.10, 7474836251038465, 80000
csv_2
id, id_2, name, unit, department, sales, special_code, others
1, 22, apple, 100, 243837463, 89.900, 0214008000000, 88899
2, 23, orange, 403, 111839281, 10.10, 7474836251038465, 80300
3, 24, banana, 909, 784635281, 30.17, 6241325251038465, 80000
代码
df1 = pd.read_csv(csv_1, header=0, dtype=str, na_filter=False)
df2 = pd.read_csv(csv_2, header=0, dtype=str, na_filter=False)
df_1.reset_index(drop=True, inplace=True)
df_2.reset_index(drop=True, inplace=True)
df1 = df_1.applymap(str.strip) #trim
df2 = df_2.applymap(str.strip) #trim
....other data processing
dfx = pd.merge(df1, df2, left_on=['id', 'id_2'], right_on=['id', 'id_2'], suffixes=['_data1', '_data2'])
print(dfx.query('name_data1 != name_data2'))
print(dfx.query('unit_data1 != unit_data2'))
print(dfx.query('dep != department'))
print(dfx.query('master_jan != jan_dh'))
print(dfx.query('sales_data1 != sales_data2'))
print(dfx.query('code != special_code'))
print(dfx.query('others_data1 != others_data2'))
输出
name_data1 unit_data1 dep... code...
1 apple, 100, 243837463...0214008000000