pandas :如何在稀疏表中选择具有非零值的列 [英] Pandas: How to select columns with non-zero value in a sparse table
本文介绍了 pandas :如何在稀疏表中选择具有非零值的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个2000列的稀疏表,我想选择一些特定的行及其非零值.我该怎么办?
I have a sparse table with 2000 column and I want to select few specific rows and their non-zero values. how can I do it?
id name e1 e2 e3 e4 . . e550 . . e1200 . e1760. . e2000
1 engine1 0 0 0 2322 1300 140 0
2 engine2 0 0 1230 0 0 2100 0
.
.
.
998000
df[df.name==engine2 & ? ]
id name e3 e1200
2 engine2 1230 2100
和
df[df.name==engine1 & ? ]
id name e4 e550 e1760
1 engine1 2322 1300 140
推荐答案
您可以过滤两次-首先通过掩码m1
行,然后通过m2
列.
上次使用 loc
:>
You can filter twice - first rows by mask m1
and then column by m2
.
Last use loc
:
m1 = (df['name'] == 'engine2')
m2 = (df[m1] != 0).all()
print (df.loc[m1,m2])
id name e3 e1200
1 2 engine2 1230 2100
这篇关于 pandas :如何在稀疏表中选择具有非零值的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文