使用Python在另一个数据范围内的一个数据帧中检查多行

发布于 2025-02-12 04:17:06 字数 826 浏览 2 评论 0原文

我的要求是检查一个数据框中的所有行是否在另一个数据范围内。在这里,我有两个数据框,如下所示:

dfactual

”

dfepped

”

在这里我要检查dfactual上的dfeppent中的所有行是否存在,如果所有人都存在,则应返回true true else elle false。 我尝试了以下解决方案:

dfActual['Bool_col'] = (dfActual.merge(dfExpected,
                                         how='left',
                                         indicator=True)
                               .eval('_merge == "both"'))
        

但是它总是返回false。任何人都可以帮我吗?

My requirement is to check whether all the rows in one dataframe present on another. Here I have two dataframe shown as below:

dfActual

dfActual

dfExpected

dfExpected

Here i want to check whether all the rows in dfExpected present on dfActual, if all present it should return true else false.
I have tried the below solution:

dfActual['Bool_col'] = (dfActual.merge(dfExpected,
                                         how='left',
                                         indicator=True)
                               .eval('_merge == "both"'))
        

But it always returns false. Could anyone please help me on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜是你 2025-02-19 04:17:06

猜测1:您想要一条新的行,该行是否发生在另一个数据框架中。然后,您可以做这样的事情:

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'NAME': ['AAA', 'BBB', 'CCC', 'CCC', 'DDD', 'AAA'],
    'AGE': [24] * 6,
    'LIMIT': [2] * 6
})

df_sub = pd.DataFrame({
    'NAME': ['AAA', 'BBB'],
    'AGE': [24] * 2,
    'LIMIT': [2] * 2
})

df['check'] = np.isin(df.values, df_sub.values).all(axis=1)
df


------------------------------
    NAME  AGE   LIMIT   check
0   AAA   24    2       True
1   BBB   24    2       True
2   CCC   24    2       False
3   CCC   24    2       False
4   DDD   24    2       False
5   AAA   24    2       True
------------------------------

猜测2:
如果您只想检查一个数据框架是否发生在另一个数据框中,则可以使用以下方式:

df_sub.isin(df).all(axis=1).all()

Guess No. 1: You want a new row that identifies whether it occurs in another data frame. Then, you could do something like this:

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'NAME': ['AAA', 'BBB', 'CCC', 'CCC', 'DDD', 'AAA'],
    'AGE': [24] * 6,
    'LIMIT': [2] * 6
})

df_sub = pd.DataFrame({
    'NAME': ['AAA', 'BBB'],
    'AGE': [24] * 2,
    'LIMIT': [2] * 2
})

df['check'] = np.isin(df.values, df_sub.values).all(axis=1)
df


------------------------------
    NAME  AGE   LIMIT   check
0   AAA   24    2       True
1   BBB   24    2       True
2   CCC   24    2       False
3   CCC   24    2       False
4   DDD   24    2       False
5   AAA   24    2       True
------------------------------

Guess No. 2:
If you just want to check, whether one data frame occurs in another, you could use this:

df_sub.isin(df).all(axis=1).all()
一个人的旅程 2025-02-19 04:17:06

它对我正常工作,您可以在我的附件映像中看到。以

< img src =“ https://i.sstatic.net/2ajlk.png” alt =“ result”>

您评论了列,然后结果还可以请参见此图像。

It is working for me correctly you can see in my attached image.Kindly check your DataFrame making mistake any where else

Result

You comment that column then also result is ok see this image.
Result after changing data

伴我老 2025-02-19 04:17:06

您可以做:

actual_list = dfActual.to_numpy().tolist()
expected_list = dfExpected.to_numpy().tolist()
all_rows_present = np.all([True if sub_list in  actual_list else False 
                for sub_list in expected_list])

You can do:

actual_list = dfActual.to_numpy().tolist()
expected_list = dfExpected.to_numpy().tolist()
all_rows_present = np.all([True if sub_list in  actual_list else False 
                for sub_list in expected_list])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文