prashant
prashant

Reputation: 271

Pandas error: ValueError: The truth value of a Series is ambiguous. on applying a function to a dataframe

I have a dataframe df and gives value error when apply a function on it.

df = pd.DataFrame({
    'ID': range(1, 4),
    'col1': [10, 5, 10],
    'col2': [15, 10, 15],
    'col3': [10, 15, 15],
    'total': [35, 30, 40]
})

print(df)

     ID  col1 col2 col3 total
0     1    10   15   10    35
1     2    5    10   15    30
2     3    10   15   15    40


def round_up(value):
    remainder = value % 5
    if remainder == 0:
        new_val = value
    if remainder == 1:
        new_val = value - 1
    if remainder == 2:
        new_val = value - 2
    if remainder == 3:
        new_val = value + 2
    if remainder == 4:
        new_val = value + 1

    return new_val

df.iloc[:, 1:-1] = df.iloc[:, 1:-1].apply(round_up)

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Upvotes: 2

Views: 410

Answers (1)

oskros
oskros

Reputation: 3305

As you are applying across several columns, you need to use applymap instead of apply. Furthermore, your function to round can be simplified as well, see below

df = pd.DataFrame({
    'ID': range(1, 4),
    'col1': [10, 5, 10],
    'col2': [15, 10, 15],
    'col3': [10, 15, 15],
    'total': [35, 30, 40]
})

def round_up(value):
    return (value + 2) // 5 * 5)

df.iloc[:, 1:-1] = df.iloc[:, 1:-1].applymap(round_up)

Upvotes: 2

Related Questions