如果使用所有标量值,则必须传递返回1的索引shell。

时间:2018-06-26 06:29:15

标签: python pandas

我的原始数据框就是这样

11S 420  3.65%
11N 580  0.19%
12S 450  6.13%

我想像原始数据框一样创建新的数据框filter1 filter2 filter3,如果索引号1和8之间的数字追加到filter1数据帧,如果索引号9和16之间的数字追加到filter2

i_r = g_e[['intrude', '8-11to10-17']]
filter1 = pd.DataFrame({"intrude":"","8-11to10-7":""})
filter2 = pd.DataFrame({"intrude":"","8-11to10-7":""})
filter3 = pd.DataFrame({"intrude":"","8-11to10-7":""})

for index1, row1 in i_r.iterrows():
    number = re.findall(r'\d{1,2}', row1.name)
    if pd.to_numeric(number) <= 8 :
         filter1.append(index1)
    if pd.to_numeric(number) <= 16:
         filter2.append(index1)
    if pd.to_numeric(number) <= 28:
         filter3.append(index1)

1 个答案:

答案 0 :(得分:0)

您应尽可能尝试使用向量化操作。在这种情况下,不需要迭代行。这是一个将整数提取为序列的示例;然后使用pd.Series.between应用过滤器:

df = i_r.copy()
df['index'] = df.index.str[:-1].astype(int)

df1 = df.loc[df['index'] <= 8]
df2 = df.loc[df['index'].between(9, 16)]
df3 = df.loc[df['index'].between(17, 28)]

print(df2)

     intrude 8-11to10-17  index
11S      420       3.65%     11
11N      580       0.19%     11
12S      450       6.13%     12