Reputation: 311
I need to print a value from a different column if certain conditions are met in an other column.
The df consist of 4 columns year, gdp, the difference and the signal
df=pd.read_excel("file.xls", names = ["year","GDP"])
gdp["diff"] = gdp["GDP"].diff(1)
signal = gdp["diff"].clip(lower = -1.0, upper=1.0)
gdp["signal"] = signal
The signal is -1 for neg values and +1 for pos values.
The condition is I have to print the year for which there are 2 consecutive negative periods.
rec_start=(gdp["signal"]==-1) & (gdp["signal"].shift(-1)==-1)
gdp["start"]=rec_start # which gives a boolean mask
rec and start are the same
year GDP diff signal start
0 1999q4 12323.3 NaN NaN False
1 2000q1 12359.1 35.8 1.0 False
2 2000q2 12592.5 233.4 1.0 False
3 2000q3 12607.7 15.2 1.0 False
4 2000q4 12679.3 71.6 1.0 False
5 2001q1 12643.3 -36.0 -1.0 False
6 2001q2 12710.3 67.0 1.0 False
7 2001q3 12670.1 -40.2 -1.0 False
8 2001q4 12705.3 35.2 1.0 False
9 2002q1 12822.3 117.0 1.0 False
Now i just have to figure out the right sintax to print the year row for 2 consecutive Trues.
trying with
foo=gdp.ix[(gdp["signal"]==-1) & (gdp["signal"].shift(-1)==-1)]["year"].iloc[0]
print(foo)
Did the trick.
Any help is much appreciated!
Upvotes: 1
Views: 125
Reputation: 7838
IIUC this should work:
df.year[np.where((df.signal==-1),(df.signal==df.signal.shift()),0).astype('bool')]
Upvotes: 1