如何比较两个 Pandas 数据帧的两个列表?

huangapple go评论69阅读模式
英文:

How to compare two lists of pandas dataframe?

问题

a = [pd.DataFrame([1,2,3])]
b = [pd.DataFrame([])]

如何检查a等于b,以便返回False?

我尝试过a==b,但它返回ValueError: Can only compare identically-labeled (both index and columns) DataFrame objects

英文:
import pandas as pd
a = [pd.DataFrame([1,2,3])]
b = [pd.DataFrame([])]

How can I check where a==b so that it returns False?

I have tried a==b but it returns ValueError: Can only compare identically-labeled (both index and columns) DataFrame objects.

答案1

得分: 1

可以使用以下代码:

all(x.equals(y) for x, y in zip(a, b))
英文:

Can use

all(x.equals(y) for x, y in zip(a, b))

答案2

得分: 1

看起来您正在尝试比较具有不同索引或列名称的数据框。如果您想比较它们的值,可以使用以下方法:

import numpy as np
are_values_equal = np.array_equal(df1.values, df2.values)
print(are_values_equal)

此外,您可以尝试像这样比较特定列:

are_columns_equal = df1['A'].equals(df2['A'])
print(are_columns_equal)

在文档中查看这两种方法:

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.equals.html
https://numpy.org/doc/stable/reference/generated/numpy.array_equal.html

英文:

Seems like you are trying to compare dataframes with different indexes or column names.
If you want to compare their values you can use:

import numpy as np
are_values_equal = np.array_equal(df1.values, df2.values)
print(are_values_equal)

Also, you can try to compare the specific columns like this:

are_columns_equal = df1['A'].equals(df2['A'])
print(are_columns_equal)

Check this two methods in docs:

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.equals.html
https://numpy.org/doc/stable/reference/generated/numpy.array_equal.html

huangapple
  • 本文由 发表于 2023年7月11日 11:53:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定