I have two data frames plotted which comes something like this . I want to get a new data frame such that whenever these two curves are crossing each other just put 1 or -1 at those places. Where positive sign could be like when the blue curve crosses the orange curve towards upward and opposite for -1.
Generated data frame with this code(for reference):
df=pd.DataFrame()
df['curve1'] = pd.DataFrame(np.sin([a/(2*np.pi) for a in range(180)])*np.random.choice([1,.8,1.2],180, p=(.5,.25,.25)), columns=["data"])
df['curve2'] = pd.DataFrame(np.sin([-a/(2*np.pi) for a in range(180)])*np.random.choice([1,.8,1.2],180, p=(.5,.25,.25)), columns=["data"])
First I am calculating the gap between those curves-
df['curve_diff'] = df['curve1']-df['curve2']
Next, I use the below command.
df.loc[df['curve_diff'] > 0 & df['curve_diff'].shift(1) == 0, 'new'] = 1.0
df.loc[df['curve_diff'] < 0 & df['curve_diff'].shift(1) == 0, 'new'] = -1.0
df.loc[df['new'] != 1.0 | df['new'] != -1.0, 'new'] = 0
These commands are not working and give this error:
TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool].
Use brackets for each condition with operators &
and |
. Like this
df.loc[(df['new'] != 1.0) | (df['new'] != -1.0), 'new'] = 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.