我有三个形状相同的数据框,如下图所示。我想遍历df1,如果df1中的单元格值大于df2中的相应单元格值,则将信号数据帧中每个单元格的值设置为1。有人可以说明如何做到这一点吗?
import pandas as pd
cols = ['ABC', 'DEF', 'GHI']
prices = [[12.22, 14.34, 98.34], [12.52, 15.34, 96.34], [13.12, 14.73, 97.47]]
prices_df1 = [[16.11, 18.12, 19.13], [16.21, 18.22, 19.23], [16.31, 18.32, 19.33]]
prices_df2 = [[12.22, 18.34, 17.34], [17.52, 18.34, 19.34], [13.12, 14.73, 16.47]]
mydates = ['09-15-2018', '09-16-2018', '09-17-2018']
signals = pd.DataFrame(index=mydates, columns=cols, data=0)
df1 = pd.DataFrame(index=mydates, columns=cols, data=prices_df1)
df2 = pd.DataFrame(index=mydates, columns=cols, data=prices_df2)
如果df1> df2,如何将信号数据帧设置为1
答案 0 :(得分:2)
您可以使用df.where
signals = signals.where(df1 < df2).fillna(1).astype(int)
signals
ABC DEF GHI
09-15-2018 1 1 0
09-16-2018 1 1 0
09-17-2018 1 1 0
答案 1 :(得分:1)
您可以使用快速布尔过滤器。
# better speed if boolean to integer conversion is separate from
# the boolean comparison
signals = (df1 > df2)
signals = signals.astype(int)
print(signals)
ABC DEF GHI
09-15-2018 1 1 0
09-16-2018 1 1 0
09-17-2018 1 1 0