-2

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].

  • Unfortunately this is not a code-writing or tutorial service, and requests for libraries or tutorials are explicitly off-topic for stack overflow. What have you tried based on your own research, and what were your results? Please provide a [mcve] so that we can offer specific help – G. Anderson May 11 '21 at 19:39
  • Guys, I am sorry for not understanding the intent of this platform, I have changed my question could you please help me to reopen the question. – user13079994 May 15 '21 at 06:02

1 Answers1

1

Use brackets for each condition with operators & and |. Like this

df.loc[(df['new'] != 1.0) | (df['new'] != -1.0), 'new'] = 0
Pawan Jain
  • 815
  • 3
  • 15