在pandas

时间:2018-04-01 08:48:26

标签: python pandas loops pandas-groupby

我试图在每个组中找到满足条件的第一个实例,然后将不同的组合并在一起。在下面的数据中,我希望第一个实例在单独的列中作为True,当'putbuy'列在数据中从每个月的0变为1时,这是从1994年到2018年。

数据:

month_x year_x  day_x   putbuy Desired putbuy
5977    2   2018    14  1           1
5978    2   2018    15  1           0
5979    2   2018    16  1           0
5980    2   2018    19  1           0
5981    2   2018    20  1           0
5982    2   2018    21  1           0
5983    2   2018    22  0           0
5984    2   2018    23  1           0  
5985    2   2018    26  0           0
5986    2   2018    27  1           0
5987    2   2018    28  0           0
5988    3   2018    1   0           0
5989    3   2018    5   0           0
5990    3   2018    6   0           0
5991    3   2018    7   0           0
5992    3   2018    8   0           0
5993    3   2018    9   0           0
5994    3   2018    12  0           0
5995    3   2018    13  0           0
5996    3   2018    14  0           0
5997    3   2018    15  0           0
5998    3   2018    16  0           0
5999    3   2018    19  1           1
6000    3   2018    20  1           0
6001    3   2018    21  0           0
6002    3   2018    22  1           0 
6003    3   2018    23  1           0
6004    3   2018    26  1           0
6005    3   2018    27  0           0
6006    3   2018    28  0           0

解决方案尝试:

grouped=options.groupby(['month_x','year_x'])

for group in grouped:
   while 'Close_x'>'pstrike':
       putb=0
    else:
        putb=1
    break       

    print(group)

我的数据集快照:

snapshot of my dataset

2 个答案:

答案 0 :(得分:0)

# create a copy of data
tmp_df = options.copy()

# take diff from previous day
tmp_df.loc[:, 'putbuy_change'] = tmp_df.groupby(['month_x', 'year_x']).putbuy.diff(1)

# keep rows where change is 1
keep = tmp_df[tmp_df.putbuy_change == 1]

# keep first instance of each month
first_ins = keep.groupby(['month_x', 'year_x']).head(1)

# add desired boolean indicator
first_ins.loc[:, 'result_col'] = True

# merge back onto data
result_df = options.merge(first_ins[['month_x', 'year_x', 'day_x', 'result_col']], on=['month_x', 'year_x', 'day_x'], how='left')

答案 1 :(得分:0)

IIUC,您可以使用idxmax查找'putbuy'最大值首次出现的索引:

df.loc[df.groupby(['year_x','month_x'])['putbuy'].idxmax(),'DO'] = 1
df['DO'] = df.DO.fillna(0).astype(int)

print(df)

输出:

      month_x  year_x  day_x  putbuy  Desired putbuy  DO
5977        2    2018     14       1               1   1
5978        2    2018     15       1               0   0
5979        2    2018     16       1               0   0
5980        2    2018     19       1               0   0
5981        2    2018     20       1               0   0
5982        2    2018     21       1               0   0
5983        2    2018     22       0               0   0
5984        2    2018     23       1               0   0
5985        2    2018     26       0               0   0
5986        2    2018     27       1               0   0
5987        2    2018     28       0               0   0
5988        3    2018      1       0               0   0
5989        3    2018      5       0               0   0
5990        3    2018      6       0               0   0
5991        3    2018      7       0               0   0
5992        3    2018      8       0               0   0
5993        3    2018      9       0               0   0
5994        3    2018     12       0               0   0
5995        3    2018     13       0               0   0
5996        3    2018     14       0               0   0
5997        3    2018     15       0               0   0
5998        3    2018     16       0               0   0
5999        3    2018     19       1               1   1
6000        3    2018     20       1               0   0
6001        3    2018     21       0               0   0
6002        3    2018     22       1               0   0
6003        3    2018     23       1               0   0
6004        3    2018     26       1               0   0
6005        3    2018     27       0               0   0
6006        3    2018     28       0               0   0