【问题标题】:Isolating Rows Of A Dataframe in a loop based on multiple conditions [duplicate]基于多个条件在循环中隔离数据帧的行[重复]
【发布时间】: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) &amp; df.sum(axis='columns').eq(1)] 应该这样做
  • 抱歉不清楚。结果将是包含 df 行的列表列表,其中列表索引 0 的行中的 Action 列将全为 1,其他列全为 0,然后列表索引 1 的 History 将全为 1,所有其他列为 0 等等。 .
  • 输入您想查看的数据框并输入问题
  • 好的,该解决方案也有效,您要发布它并接受它。谢谢

标签: python while-loop data-manipulation multilabel-classification


【解决方案1】:

您不需要循环。 你很少需要 Pandas 的循环。 如果您根据条件选择行,则应使用布尔索引。

在你的情况下,那是:

df.loc[df.sum(axis='columns').eq(1)]

举个例子:

pandas.DataFrame({
    'A': [1, 0, 0, 0, 0, 1, 1, 0, 0],
    'B': [0, 1, 0, 0, 1, 0, 1, 0, 0],
    'C': [0, 0, 1, 0, 1, 0, 0, 1, 0],
    'D': [0, 0, 0, 1, 0, 1, 0, 1, 0],
}).loc[lambda df: df.sum(axis='columns').eq(1)].values.tolist()

哪些输出:

[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]

【讨论】:

  • 我正在尝试构建一个列表列表,其中仅包含非多标签行我不需要为此循环遍历每一列吗?您的原始答案是正确的,但我需要在我看到 Western 的地方添加一个变量,以便下次循环时捕获 History 等等......
  • @DigitalMoniker 不。没有循环。如果需要,可以在上面的命令末尾添加.values.tolist()
  • 哇,我现在刚刚用 seaborn 绘制了它,我发现没有多标签......这太棒了。是因为.eq?我不熟悉它,但我会检查一下。谢谢!
  • @DigitalMoniker 如果您使用的是 seaborn,则不需要列表。
  • 我刚刚使用 seaborn 调查了在您的原始答案之后返回的数据框。我提到了一个列表列表,因为我之前使用过该技术来捕获基于多个不同条件的过滤 df 结果是全部
猜你喜欢
  • 2019-10-13
  • 1970-01-01
  • 2018-04-05
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多