擅长:python、mysql、java
<p>除了<code>numpy.where</code>之外,您还可以使用<code>loc</code>函数和条件来实现这一点,如下所示:</p>
<pre><code>df.loc[df.myopen >= df.myclose, 'new_col'] = df.myopen X 6 + 4 * df.High + (df.High - df.myclose)
df.loc[df.myopen < df.myclose, 'new_col'] = df.myclose X 3 + 4 * df.myclose + (df.myclose - df.myopen)
</code></pre>
<p>或者,如果函数变得更复杂,可以使用<code>dataframe.apply</code>。e、 g</p>
<pre><code>def open_close_calculation(df):
if df.myopen >= df.myclose:
return df.myopen X 6 + 4 * df.High + (df.High - df.myclose)
else:
return df.myclose X 3 + 4 * df.myclose + (df.myclose - df.myopen)
df['new_col'] = df.apply(open_close_calculation, axis=1)
</code></pre>