0

I am trying to add a new field to a dataframe using a function. But this is failing with the error:

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

Any help is appreciated

import pandas as pd

    data = {
      "calories": [420, 380, 390],
      "duration": [50, 40, 45]
    }
    
    df = pd.DataFrame(data)
    
    #function
    def test_function(df):
        if df['calories'] > 200:
            output = "OutputA"
        else:
            output = "OutputB"
        return output
    
    #adding new field and assigning to function
    df["new_field"] = test_function(df)
    
    print(df)
Rutnet
  • 1,533
  • 5
  • 26
  • 48

0 Answers0