Reputation: 388
Here is my issue
I have this DataFrame :
df = pd.DataFrame({'Date':[1,2,3],
'Paul':[8,10,13],
'Mathieu':[18,20,2],
'Jacques':[2,1,70]})
df = df.set_index('Date')
My goal is to create an IF statement with those conditions : IF the last value of the rolling mean 2 days is < the rolling mean 3 days and the fist value (Day 1) is > the last value (Day 3) then print the name of the column.
This is what I started :
def test(data):
end = data.iloc[-1]
start = data.iloc[0]
end_rolling_2D = data.rolling(2).mean().iloc[-1]
end_rolling_3D = data.rolling(3).mean().iloc[-1]
if end_rolling_2D < end_rolling_3D and start > end :
print(data.columns)
But I have this Error :
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
And I don't really know how to solve that.I know that the aswer should be 'Mathieu' only because he's the only one that met the conditions.
I am very new to Python, so if anyone has an idea to solve that, you are welcome!
Thank you.
Upvotes: 0
Views: 256
Reputation: 20669
start
, end
, end_rolling_2D
and end_rolling_3D
are Series
object, use pd.Series.all()
Replace
if end_rolling_2D > end_rolling_3D and start > end
with
if ((end_rolling_2D>end_rolling_3D) & (start>end)).all()
To get the location where the condition is satisfied use boolean indexing
mask = (end_rolling_2D<end_rolling_3D) & (start>end)
print(df.columns[mask])
# Index(['Mathieu'], dtype='object')
Upvotes: 2
Reputation: 1622
Change your function to:
def test(data):
end = data.iloc[-1]
start = data.iloc[0]
end_rolling_2D = data.rolling(2).mean().iloc[-1]
end_rolling_3D = data.rolling(3).mean().iloc[-1]
if ((end_rolling_2D > end_rolling_3D) & (start > end)).all() :
print(data.columns)
Please note that I have added brackets. And replaced and
by &
However, I do not expect any data which satisfies your criteria.
Upvotes: 0