即使使用位运算符和适当的括号

发布于 2025-02-09 02:26:53 字数 796 浏览 4 评论 0 原文

我试图根据其某些列(本文Col1-4)详细阐述DF,并根据函数中定义的某些条件添加新列。 该代码可能比简单英语的解释更清楚:

def get_new_col(df):
    if (df['col1'] == 0) | (df['col2'] == 0):
        first_half = 0
    else:
        first_half = min(df['col1'], df['col2'])
    if (df['col3'] == 0) | (df['col4'] == 0):
        second_half = 0 
    else:
        second_half = min(df['col3'], df['col4'])
    return first_half + second_half

df['new_col'] = get_new_col(df)

我的问题是我得到了 valueerror:系列的真实价值是模棱两可的。使用A.Empty,A.Bool(),A.Item(),A.Any()或A.all()。,即使我正确地(我认为?)语句并使用位运算符|代替,如此其他 thread

关于如何解决这个问题有什么想法吗?

I am trying to elaborate a df based on some of its columns (col1-4 herein), and to add a new column based on certain conditions defined in a function.
The code might be clearer than an explanation in plain english:

def get_new_col(df):
    if (df['col1'] == 0) | (df['col2'] == 0):
        first_half = 0
    else:
        first_half = min(df['col1'], df['col2'])
    if (df['col3'] == 0) | (df['col4'] == 0):
        second_half = 0 
    else:
        second_half = min(df['col3'], df['col4'])
    return first_half + second_half

df['new_col'] = get_new_col(df)

My problem is that I am getting a ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()., even though I am properly (I think?) bracketing the conditions of the if statements and using the bitwise operator | instead of or, as suggested in this other thread.

Any idea on how to solve this?

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

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

发布评论

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

评论(1

梦幻的心爱 2025-02-16 02:26:53

iiuc,您需要 dataframe.apply 在行

def get_new_col(row):
    if (row['col1'] == 0) | (row['col2'] == 0):
        first_half = 0
    else:
        first_half = min(row['col1'], row['col2'])
    if (row['col3'] == 0) | (row['col4'] == 0):
        second_half = 0
    else:
        second_half = min(row['col3'], row['col4'])
    return first_half + second_half

df['new_col'] = df.apply(get_new_col, axis=1)

问题上,您遇到的是 df ['col1'] == 0 返回一个布尔序列,该系列无法通过如果语句。

使用 df.apply(get_new_col,axis = 1),我们只将数据框的一行传递到 get_new_col 函数。

要在没有应用的情况下执行相同的操作,您可以尝试

def get_new_col(df):
    first_half = df[['col1', 'col2']].min(axis=1)
    first_half = first_half.mask(df[['col1', 'col2']].eq(0).any(axis=1), 0)

df['new_col'] = get_new_col(df)

IIUC, you need DataFrame.apply on rows

def get_new_col(row):
    if (row['col1'] == 0) | (row['col2'] == 0):
        first_half = 0
    else:
        first_half = min(row['col1'], row['col2'])
    if (row['col3'] == 0) | (row['col4'] == 0):
        second_half = 0
    else:
        second_half = min(row['col3'], row['col4'])
    return first_half + second_half

df['new_col'] = df.apply(get_new_col, axis=1)

Problem you get is that df['col1'] == 0 returns a boolean Series which couldn't be accepted by if statement.

With df.apply(get_new_col, axis=1), we are only passing one row of dataframe to get_new_col function.

To do the same without apply, you can try

def get_new_col(df):
    first_half = df[['col1', 'col2']].min(axis=1)
    first_half = first_half.mask(df[['col1', 'col2']].eq(0).any(axis=1), 0)

df['new_col'] = get_new_col(df)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文