Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除值计数满足条件的列(a)_Python_Pandas_Dataframe - Fatal编程技术网

Python 删除值计数满足条件的列(a)

Python 删除值计数满足条件的列(a),python,pandas,dataframe,Python,Pandas,Dataframe,我有一个df,格式如下,有70000列和540行。所有值均为0.0、0.5或1.0 VAR 1_139632_G 1_158006_T 1_172595_A 1_564650_A 1_564652_G \ SRR4216489 0.5 0.5 0.5 0.5 0.5 SRR4216786 0.5 0.5 0.5 0.

我有一个df,格式如下,有70000列和540行。所有值均为0.0、0.5或1.0

 VAR         1_139632_G  1_158006_T  1_172595_A  1_564650_A  1_564652_G  \
 SRR4216489         0.5         0.5         0.5         0.5         0.5   
 SRR4216786         0.5         0.5         0.5         0.5         0.5   
 SRR4216628         0.5         0.0         1.0         0.0         0.0   
 SRR4216456         0.5         0.5         0.5         0.5         0.5   
 SRR4216393         0.5         0.5         0.5         0.5         0.5   
我想删除“0.5”值的数量仅比行数少1的所有列。到目前为止,我已经试过了

total_samples = len(df.index) # Gets the number of rows
df_col_05 = df[df == 0.5].count() # returns a df with column-wise counts
df_col_05 = df_col_05.where(df_col_05 < (total_samples-1)) #replaces with Nan where the condition isn't met
total_samples=len(df.index)#获取行数
df_col_05=df[df==0.5].count()#返回具有按列计数的df
df_col_05=df_col_05。其中(df_col_05<(样本总数-1))#在不满足条件的情况下替换为Nan
我想要的是我的原始df在df_col_05的值>=(总样本数-1)的地方删除所有列,所以基本上在“df_col_05”有NaN的地方删除所有列,但不确定如何执行

我相信这对任何比我更有熊猫经验的人来说都是很容易的(我几天前就开始了)

你可以用它来过滤列,更好的方法是在
DataFrame
中获得
True
s的
size

#if first column is not index set it
df = df.set_index('VAR')
df1 = df.loc[:, (df == 0.5).sum() >= len(df.index)-1]
样本

#changed values in last 2 columns
print (df)
          VAR  1_139632_G  1_158006_T  1_172595_A  1_564650_A  1_564652_G
0  SRR4216489         0.5         0.5         0.5         0.0         0.0
1  SRR4216786         0.5         0.5         0.5         0.0         0.5
2  SRR4216628         0.5         0.0         1.0         0.0         0.0
3  SRR4216456         0.5         0.5         0.5         0.5         0.5
4  SRR4216393         0.5         0.5         0.5         0.5         0.5

print (df[df == 0.5].count())
VAR           0
1_139632_G    5
1_158006_T    4
1_172595_A    4
1_564650_A    2
1_564652_G    3
dtype: int64

print ((df == 0.5).sum())
VAR           0
1_139632_G    5
1_158006_T    4
1_172595_A    4
1_564650_A    2
1_564652_G    3
dtype: int64

另一种没有设置索引的解决方案,只需要定义输出中始终需要的列:

m = (df == 0.5).sum() >= len(df.index)-1
print (m)
VAR           False
1_139632_G     True
1_158006_T     True
1_172595_A     True
1_564650_A    False
1_564652_G    False
dtype: bool

need_cols = ['VAR']
m.loc[need_cols] = True
print (m)
VAR            True
1_139632_G     True
1_158006_T     True
1_172595_A     True
1_564650_A    False
1_564652_G    False
dtype: bool

print (df.loc[:, m])
          VAR  1_139632_G  1_158006_T  1_172595_A
0  SRR4216489         0.5         0.5         0.5
1  SRR4216786         0.5         0.5         0.5
2  SRR4216628         0.5         0.0         1.0
3  SRR4216456         0.5         0.5         0.5
4  SRR4216393         0.5         0.5         0.5
类似的解决方案是分别过滤列,然后选择:

print (df[df.columns[m]])
          VAR  1_139632_G  1_158006_T  1_172595_A  1_564652_G
0  SRR4216489         0.5         0.5         0.5         0.0
1  SRR4216786         0.5         0.5         0.5         0.5
2  SRR4216628         0.5         0.0         1.0         0.0
3  SRR4216456         0.5         0.5         0.5         0.5
4  SRR4216393         0.5         0.5         0.5         0.5

伟大的这就解决了问题-非常感谢!作为一名熊猫新手,您是否介意澄清一些代码正在做什么。这是“df.loc[:,”吗?它同时指向所有列并跨所有行?我想它需要一个大小匹配的布尔数组,带有一个共享索引,“m”就是其中的一个。是的,没错。经典的布尔索引更简单,通过布尔掩码删除行,如
df=df[df['col']<5]
。但是删除列需要loc,首先
表示所有行,然后布尔掩码根据条件删除列。掩码的大小必须与df相同,否则会出错。祝熊猫好运,如果需要更多解释,请告诉我。周末愉快!
print (df[df.columns[m]])
          VAR  1_139632_G  1_158006_T  1_172595_A  1_564652_G
0  SRR4216489         0.5         0.5         0.5         0.0
1  SRR4216786         0.5         0.5         0.5         0.5
2  SRR4216628         0.5         0.0         1.0         0.0
3  SRR4216456         0.5         0.5         0.5         0.5
4  SRR4216393         0.5         0.5         0.5         0.5