【问题标题】:List of column names where value > X for selected rows选定行的 value > X 的列名列表
【发布时间】:2019-04-02 13:29:01
【问题描述】:

我有一个 11 行 x 17604 列的数据框。行数可能会随着我更改聚类而变化。

    B42D2033/26 G02B27/2214 G02F1/133753    G02F1/133707    G02F1/1341  G02F1/1339  G02F1/133371    G02B6/005   C08G73/12   G02F1/1303  ... G06F17/30035    G06F21/629  B65B3/26    E04D13/00   G06F17/30952    G07C9/00912 F02C9/28    G06F17/28   G06F17/30964    G06F21/82
Cluster                                                                                 
C1  0.000000    1.000000    0.000000    0.000000    0.000000    1.000000    0.000000    0.000000    0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
C10 0.000000    3.250000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
C11 0.020619    1.149485    0.262887    0.829897    0.551546    1.030928    0.082474    1.175258    0.005155    0.216495    ... 0.005155    0.010309    0.005155    0.005155    0.005155    0.005155    0.005155    0.005155    0.005155    0.005155
C2  0.000000    1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
C3  0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
C4  0.055556    13.500000   8.333333    24.555556   13.166667   26.666667   3.277778    4.222222    0.000000    2.388889    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
C5  0.000000    0.750000    0.000000    0.000000    0.000000    0.500000    0.000000    0.250000    0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
C6  0.032258    3.451613    0.000000    0.000000    0.000000    0.387097    0.000000    0.064516    0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
C7  0.000000    0.000000    0.250000    0.000000    0.000000    0.250000    0.000000    0.000000    0.000000    1.500000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
C8  0.000000    0.076923    0.153846    0.346154    0.000000    0.884615    0.461538    0.192308    0.038462    0.076923    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
C9  0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    ... 0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000

我想根据列中的值为每个集群生成一个字典或系列。例如,值!=0 可能出现的所有列,字典形式如下:

{'C1', ['G02B27/2214', 'G02F1/1339']}

如何为值等于“某个值”或某个值范围的每个集群行生成一个系列?

我确实看过 Select rows from a DataFrame based on values in a column in pandas,但该解决方案不适用于一行中的所有列。

编辑: 我意识到我可以转置df 并执行以下操作:

df_clusters.T[df_clusters.T['C1']>0]

它返回一个 df ,其中 'C1' 大于 0 的每一行。我想我可以删除其他集群列,但我认为这不是最好的解决方案。

【问题讨论】:

  • 你的标题是> 0,你的问题是!=0。哪种情况?
  • 确切的条件无关紧要,可以是 !=0, >=1 等等

标签: python python-3.x pandas


【解决方案1】:

想法是为每个条件创建值的索引,然后创建新的 DataFrame 并获取列表中每个 indices 的列表,然后转换为 dict

i, c = np.where(df > 0)
d = pd.DataFrame({'a':df.index[i], 'b':df.columns[i]}).groupby('a')['b'].apply(list).to_dict()
print (d)

另一种解决方案是使用DataFrame.stackDataFrame.melt 进行整形,按boolean indexingDataFrame.query 过滤,最后使用dict 创建lists:

s = df.stack()
d = s[s > 0].reset_index().groupby('Cluster')['level_1'].apply(list).to_dict()

d = (df.reset_index()
       .melt('Cluster', value_name='v1', var_name='v2')
       .query('v1 > 0')
       .groupby('Cluster')['v2']
       .apply(list)
       .to_dict())

【讨论】:

  • 前两个解决方案效果很好,但最后一个为.query('value > 0') 行返回一个类型错误(TypeError: '>' not supported between 'str' and 'int')
  • @Britt - 在 pandas 0.24.2 中测试,但可以在 melt 函数中明确设置新列,编辑答案。
  • 解决方案 3 的更新代码现在可以使用。谢谢@Jezrael
【解决方案2】:

试试:

df.apply(lambda x: df.columns[x>0].tolist(), axis = 1).to_dict()

【讨论】:

  • 我使用的是pandas 0.24.2版,这行代码导致错误:IndexError: ('boolean index did not match indexed array along dimension 0; dimension is 17607 but corresponding boolean dimension is 17604', 'occurred at index C1')
  • 很遗憾,我没有关于您的数据框的足够信息。所有值都是浮动的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多