英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论