确认两个 pandas 数据框是否相等? [英] Confirming equality of two pandas dataframes?

查看:101
本文介绍了确认两个 pandas 数据框是否相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何断言以下两个数据帧df1df2是相等的?

How to assert that the following two dataframes df1 and df2 are equal?

import pandas as pd
df1 = pd.DataFrame([1, 2, 3])
df2 = pd.DataFrame([1.0, 2, 3])

df1.equals(df2)的输出是False. 到目前为止,我知道两种方法:

The output of df1.equals(df2) is False. As of now, I know two ways:

print (df1 == df2).all()[0]

df1 = df1.astype(float)
print df1.equals(df2)

似乎有点混乱.有没有更好的方法进行这种比较?

It seems a little bit messy. Is there a better way to do this comparison?

推荐答案

您可以使用assert_frame_equal而不检查列的dtype.

You can use assert_frame_equal and not check the dtype of the columns.

# Pre v. 0.20.3
# from pandas.util.testing import assert_frame_equal

from pandas.testing import assert_frame_equal

assert_frame_equal(df1, df2, check_dtype=False)

这篇关于确认两个 pandas 数据框是否相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆