I'm quite a beginner in python and I try to convert an excel sheet into a pandas dataframe.
I have following if statement in excel: =IF(MOD(F183,2)=1, "PAUSE", "BREAK")
The column F183 represents 8 quarters: 1,2,3,4,5,6,7,8
I started to define a function and then to put it into the dataframe with .apply, but I'm quite struggling with setting up the function in the first place. That's what I have so far:
def pause(i):
i = df['Quarter']
if i.mod(2) == 1:
return "PAUSE"
else:
return "BREAK"
When I call the function with e.g. pause(2), I get a value error.
Thank you for any suggestions
CodePudding user response:
You could this - where column F in your spreadsheet is represented by df['Quarter']
import numpy as np
df['PAUSE_BREAK']=np.where(df['Quarter']%2==0,'BREAK','PAUSE')
Edit: I would slightly edit the function to take on values only, then apply 'map' that maps that function to each element of the series df['Quarters']
def pause(i):
if i%2 == 1:
return "PAUSE"
else:
return "BREAK"
df.Quarter.map(pause)
Edit2: with the apply function, I would pass in this lambda function - seems to work as well
df.apply(lambda row:'PAUSE' if row['Quarter']%2==1 else 'BREAK',axis=1)