繁体   English   中英

Pandas Dataframes-根据df1中的条件填充df2

[英]Pandas Dataframes - Populating df2 based on criteria in df1

我有这个数据框(df1),我想根据该数据框将数据输入到另一个数据框(df2)。 如果df1的值大于54,我希望df2中的同一行在“购买”列下为“购买”,如果不是,我希望它在“出售”列下为“出售”。 我知道这听起来很容易,但是由于某些原因,当我在下面的代码中执行此操作时,它会基于df1中的最后一个值来设置df2中的所有值。

for x in df1['A']:
    if x > 54:
       df2['Buy'] = "Buy"

    else:
       df2['Sell'] = "Sell"

DF1:

    Date
    2011-08-26     53.024284
    2011-08-29     55.454285
    2011-08-30     55.464287
    2011-08-31     55.795715
    2011-09-01     55.117142
    2011-09-02     53.534286

df2:

            Buy  Hold  Sell
Date
2011-08-26  0.0    0.0   0.0
2011-08-29  0.0    0.0   0.0
2011-08-30  0.0    0.0   0.0
2011-08-31  0.0    0.0   0.0
2011-09-01  0.0    0.0   0.0
2011-09-02  0.0    0.0   0.0

首先需要两个索引相同,然后可以在另一个DataFrame df2使用由df1中的条件创建的布尔掩码:

m = df1['A'] > 54
df2['Buy'] = df2['Buy'].mask(m, "Buy")
df2['Sell'] = df2['Sell'].mask(~m, "Sell")

assign相同:

df2 = df2.assign(Buy= df2['Buy'].mask(m, "Buy"),Sell = df2['Sell'].mask(~m, "Sell"))

要么:

df2.loc[m, 'Buy'] = "Buy"
df2.loc[~m, 'Sell'] = "Sell"

print (df2)
            Buy  Hold  Sell
Date                       
2011-08-26    0   0.0  Sell
2011-08-29  Buy   0.0     0
2011-08-30  Buy   0.0     0
2011-08-31  Buy   0.0     0
2011-09-01  Buy   0.0     0
2011-09-02    0   0.0  Sell

如果索引不同,请使用reindex

m = (df1['A'] > 54).reindex(df2.index, fill_value=False)

使用np.where

df2['Buy'] = np.where(df1['A']>54,'Buy',df2['Buy'])
df2['Sell'] = np.where(df1['A']<54,'Sell',df2['Sell'])

df.where

df2['Buy'] = df2['Buy'].where(df1['A']<54,'Buy')
df2['Sell'] = df2['Sell'].where(df1['A']>54,'Sell')

输出:

Buy  Hold  Sell
Date                       
2011-08-26  0.0   0.0  Sell
2011-08-29  Buy   0.0   0.0
2011-08-30  Buy   0.0   0.0
2011-08-31  Buy   0.0   0.0
2011-09-01  Buy   0.0   0.0
2011-09-02  0.0   0.0  Sell

如果索引不相同,则必须按照@jezrael在其解决方案中建议的方法重新索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM