python - 所选行的值 > X 的列名称列表

标签 python python-3.x pandas

我有一个 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 的每一行。我想我可以删除其他簇列,但我不认为这是最好的解决方案。

最佳答案

想法是为每个条件创建值的索引,然后创建新的 DataFrame 并获取列表中每个索引的列表,然后转换为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对于 reshape ,按boolean indexing过滤或DataFrame.query最后使用 dict 创建 list:

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())

关于python - 所选行的值 > X 的列名称列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55476028/

相关文章:

python-3.x - 有什么方法可以找到多个图像的边界并单独裁剪它们?

python - 为什么在大循环中使用按位运算时会得到这么长的运行时间?

python - ElementTree 查找返回 'None' ?

python - Spyder (Python 3.7) 启动时出现一个黑色窗口。我该如何修复它?

python - CNTK:访问经过训练的模型数据

python - pandas 在 groupby 上设置 withcopywarning

python - 在 Pandas 的一列中切片字符串

python - Pandas 数据框到以行索引为值的字典?

Python 2.6.5 支持 Unicode?为什么 listdir() 不显示 Python 3.1.2 却显示 Unicode?

python - 错误 "Could not find a version that satisfies the requirement flask (from versions: ) No matching distribution found for flask"