我有一个熊猫数据框架,如下所示。它有11列,其中只包含0和1,还有一列包含一些值,最后一列是标识符。我正面临着数据帧操作的问题。
在这种情况下,我需要根据“values”列选择前11行。(松散约束)
但棘手的部分是,我必须以这样一种方式选择行,即在这11行中不会得到任何零列。(硬约束)。
因此,我需要根据值选择前11行,并确保所有列都非零。每列中至少有一个值应为1。
我正在寻找一些通用的解决方案,因为值列中的值会发生变化,但我的目标是根据值选择11行,并确保非零列是必须的。
有什么想法吗?
a b c d e f g h i j k values ID
0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.193744 1
0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 1.193744 2
0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.193744 3
0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.193744 4
0.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.193744 5
0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.193744 6
0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.193744 7
0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.633150 8
0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.633150 9
0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.633150 10
0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.633150 11
0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.633150 12
0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.033640 13
0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.033640 14
0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.033640 15
1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.033640 16
0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.033640 17
1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.033640 18
0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.033640 19
1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.033640 20
0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.033640 21
0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.033640 22
0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.033640 23
0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.033640 24
1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.033640 25
1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 -0.279495 26
1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 -3.013531 27
1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 -3.013531 28发布于 2021-10-05 11:30:04
使用自定义函数-首先排序values,然后通过DataFrame.iloc选择前11列,通过DataFrame.astype将浮点数转换为布尔True和Falses,因此可以使用any测试每个N个块是否至少有一个1,因此通过NaN填充的行移位由0替换,转换为布尔值,spo可能测试是否所有行都有1 DataFrame.all。
最后获取第一个True的索引,但如果所有False都返回0格式的Series.idxmax,因此添加了if-else语句。
最后一次按索引过滤top11:
def top_val_with_at_least_one_1(df, N):
df = df.sort_values('values', ascending=False, ignore_index=True, kind='mergesort')
m = (df.iloc[:, :11]
.astype(bool)
.rolling(N)
.apply(lambda x: x.any())
.fillna(0)
.astype(bool)
.all(axis=1))
if m.any():
idx = m.idxmax()
return df.loc[idx-N+1:idx]
else:
return pd.DataFrame()
print (top_val_with_at_least_one_1(df, 11))
a b c d e f g h i j k values ID
5 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.193744 6
6 0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.193744 7
7 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.633150 8
8 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.633150 9
9 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.633150 10
10 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.633150 11
11 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.633150 12
12 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.033640 13
13 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.033640 14
14 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.033640 15
15 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.033640 16发布于 2021-10-05 11:21:15
尝试:
def get_desired_subset(top_rows):
for i in range(len(df)):
df_subset = df.loc[i:i+top_rows-1].reset_index(drop=True)
if (df_subset>0).any().all():
return df_subset
print('No such subset found that satisfies all constraints.')
return pd.DataFrame()
top_rows = 3
df = df[(df>0).any(axis=1)].sort_values(subset=['values'], ascending=False).reset_index(drop=True)
if len(df)>= top_rows:
desired_df = get_desired_subset(3)
else:
print('No such subset found that satisfies all constraints.')https://stackoverflow.com/questions/69449403
复制相似问题