假设我的DataFrame'候选人'看起来如下:
HnL close r_value sector_code
Equity(36742 [TREE]) 1.0 279.25 0.974858 103.0
Equity(16820 [TTWO]) 1.0 118.89 0.973719 311.0
Equity(45521 [RNG]) 1.0 47.20 0.972306 311.0
Equity(49242 [PYPL]) 1.0 77.70 0.985572 103.0
Equity(25339 [ISRG]) 1.0 393.76 0.985117 206.0
Equity(8613 [CHDN]) 1.0 217.30 0.989951 102.0
最左边的列是索引。
我想要的是:我将取出一些行来构建另一个DataFrame。我使用以下内容获取行:
for index, row in candidates.iterrows():
... do some calculation here,
... if fulfills my criteria, add this row to build
... the new DataFrame with the same index
假设1.,3.和6.行满足我的标准,在重新生成新的DataFrame之后,它应该如下所示:
HnL close r_value sector_code
Equity(36742 [TREE]) 1.0 279.25 0.974858 103.0
Equity(45521 [RNG]) 1.0 47.20 0.972306 311.0
Equity(8613 [CHDN]) 1.0 217.30 0.989951 102.0
答案 0 :(得分:0)
您可以使用列表来存储要添加到新数据帧的所有行的索引,然后使用.loc
对这些行进行子集化。
keep_rows = list()
for index, row in candidates.iterrows():
# define a condition that is either True or False based on row values
# for example, "condition = row['close'] > 200"
if condition:
keep_rows.append(index)
new_df = candidates.loc[keep_rows,:]