【发布时间】:2021-04-03 04:15:41
【问题描述】:
所以我最近问了一个与此相关的问题,虽然当时答案很简单(我没有使用特定的专栏),但这次我没有那个专栏。 Here is the OP。那里提供的额外答案都没有实际工作:/
当您想要隔离包含给定类的 1 和其他类的零的行时,问题在于多标签数据框。到目前为止,这是我拥有的代码,但它会循环到无穷大并导致 colab 崩溃。
在这种情况下,我只想要那个 Action 行,但我也试图循环它,所以我将附加所有值为 1 的 Action 和值为 0 的 column_list 下一个 History 1 所有其他 0 等等...
链接上提供的选项再次给我一个The truth of the answer is ambiguous 错误
Index | Drama | Western | Action | History |
0 1 1 0 0
1 0 0 0 1
2 0 0 1 0
# Column list to be popped
column_list = list(balanced_df.columns)[1:]
single_labels = []
i=0
# 28 columns total
while i < 27:
# defining/reseting the full column list at the start of each loop
column_list = list(balanced_df.iloc[:,1:])
# Pop column name at index i
x = column_list.pop(i)
# storing the results in a list of lists
# Filters for the popped column where the column is 1 & the remaining columns are set to 0
single_labels.append(balanced_df[(balanced_df[x] == 1) & (balanced_df[column_list]==0)])
# incriment the column index number for the next run
i+=1
这里的输出类似于
single_labels[0]
Index | Drama | Western | Action | History |
2 0 0 1 0
single_labels[1]
Index | Drama | Western | Action | History |
1 0 0 0 1
【问题讨论】:
-
你想要的结果是什么?
-
从另一个问题中的 cmets,
df.loc[df['Western'].eq(1) & df.sum(axis='columns').eq(1)]应该这样做 -
抱歉不清楚。结果将是包含 df 行的列表列表,其中列表索引 0 的行中的 Action 列将全为 1,其他列全为 0,然后列表索引 1 的 History 将全为 1,所有其他列为 0 等等。 .
-
输入您想查看的数据框并输入问题
-
好的,该解决方案也有效,您要发布它并接受它。谢谢
标签: python while-loop data-manipulation multilabel-classification