【发布时间】:2022-10-31 16:31:58
【问题描述】:
我有一个以下数据框,其中我有一个基于某些条件的索引位置列表,所以只想根据索引位置创建新的数据框并检查一些条件。
df = pd.DataFrame()
df['index'] = [ 0, 28, 35, 49, 85, 105, 208, 386, 419, 512, 816, 888, 914, 989]
df['diff_in_min'] = [ 5, 35, 42, 46, 345, 85, 96, 107, 119, 325, 8, 56, 55, 216]
df['val_1'] = [5, 25, 2, 4, 2, 5, 69, 6, 8, 7, 55, 85, 8, 67]
df['val_2'] = [8, 89, 8, 5, 7, 57, 8, 57, 4, 8, 74, 65, 55, 74]
re_ind = list(np.where(df['diff_in_min'] >= 300))
re_ind = [np.array([85, 512], dtype='int64')]
只是我想根据 re_ind 位置创建另一个数据框,例如:
first_df = df[0:85]
another_df = [85:512]
last_df = [512:]
以及我想检查一个条件的每个数据框
count = 0
temp_df = df[:re_ind[0]]
if temp_df['diff_in_min'].sum() > 500:
count += 1
temp_df = df[re_ind[0]:re_ind[1]]
if temp_df['diff_in_min'].sum() > 500:
count += 1
if temp_df = df[re_ind[1]:]
if temp_df['diff_in_min'].sum() > 500:
count += 1
如何使用 for 循环使用现有数据框创建新数据框来做到这一点?
【问题讨论】: