[英]filter pandas dataframe by row value
I know how to filter a dataframe by column value: 我知道如何按列值过滤数据框:
import pandas as pd
import numpy as np
from numpy.random import randn
np.random.seed(101)
df = pd.DataFrame(randn(5,4),index='A B C D E'.split(),columns='W X Y Z'.split())
print(df)
# show only rows where 'W' is positive
# here, the row for 'C' will be deleted, since df['W']['C']<0
df[df['W']>0]
But how do I filter by row value, eg 'B'>0? 但是,如何按行值过滤,例如'B'> 0?
Since df['X']['B']<=0 and df['Y']['B']<=0, I would like to delete columns X and Y. I tried the following code, but it reports an error: 由于df ['X'] ['B'] <= 0和df ['Y'] ['B'] <= 0,因此我想删除X和Y列。我尝试了以下代码,但报告一个错误:
df.loc[df.loc['B']>0]
You should using the filter on columns 您应该在列上使用过滤器
df.loc[:,df.loc['B',:]>0]
Out[67]:
W Z
A 2.706850 0.503826
B 0.651118 0.605965
C -2.018168 -0.589001
D 0.188695 0.955057
E 0.190794 0.683509
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.