1

I am trying to apply function which refers to 2 columns to a pandas dataframe. One column that that the function is referring to has string variables, other integers.

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

Below is the example of what I have tried so far

def sd2_offshore(gas_rate,platform):
    if platform=='Stage2':
       if 0<gas_rate<900:
          return 5
       elif gas_rate>900:
          return 6
       else:
          return 0
     else:
        return 0

df2['Offshore SD2']=df2.apply(lambda row : sd2_offshore(df2['Gas_Rate_avg'],df2['Platform']))

df2['Platform'] contains string values.

df2['Gas_Rate_avg'] contains integer values

Thanks a lot in advance!

Edit

Adding example of the data below

Index Platform Gas_Rate_avg
0     Stage2   300
1     Stage2   0
2     Stage2   1100
3     Stage2   1200
4     Stage1   500

Expected output:

Index Platform  Gas_Rate_avg    Offshore_SD2
0     Stage2    300             5.00
1     Stage2    0               0.00
2     Stage2    1100            6.00
3     Stage2    1200            6.00
4     Stage1    500             0.00
  • You can't do this: `0 0) & (gas_rate < 900))` – DavideBrex Jun 21 '20 at 19:47
  • seems like the gas_rate related conditions work the way I have written the code, it is when I introduce platform condition (the first if statement) it raises the Value Error – Masail Guliyev Jun 22 '20 at 06:20
  • you might need `.isin()`. see [here](https://stackoverflow.com/a/19960116/13328010) – DavideBrex Jun 22 '20 at 06:38
  • @DavideBrex I have just tried rewriting the function this way, but still got a Value Error :/ def sd2_offshore(df): platforms=['Stage1'] if df['Platform'].isin(platforms): if 0900: return 6 else: return 0 else: return 0 df2['Offshore SD2']=df2.apply(lambda row : sd2_offshore(df2)) – Masail Guliyev Jun 22 '20 at 06:54
  • Ow wow, it looks ,messy when I post in comments, couldn't find a better way, sorry... @DavideBrex – Masail Guliyev Jun 22 '20 at 06:58
  • no problem! If you could edit your question and add an example of df2 (with some rows, and not as an image please) I can reproduce your situation and try to find a solution! – DavideBrex Jun 22 '20 at 07:05
  • 1
    @DavideBrex I have edited my original post with sample data and expected output – Masail Guliyev Jun 22 '20 at 08:38

1 Answers1

2

You were missing axis=1 in apply. Try in this way:

def sd2_offshore(row): 
    if row["Platform"] == "Stage2": 
        if 0<row['Gas_Rate_avg']<900: 
            return 5 
        elif row['Gas_Rate_avg']>900:
            return 6
        else: 
            return 0 
    else:
        return 0 
    
df2['Offshore SD2'] = df2.apply(lambda row : sd2_offshore(row), axis=1)
df2

Output(df2):

      Platform  Gas_Rate_avg    Offshore SD2
Index           
0     Stage2    300            5
1     Stage2    0              0
2     Stage2    1100           6
3     Stage2    1200           6
4     Stage1    500            0
DavideBrex
  • 2,374
  • 1
  • 10
  • 23