1

我希望完全需要删除所有用零填充的行和列。

但它只返回这个。

      Box_deatils    1    5
0           1  0.0  0.0
1           2  1.0  0.0
2           3  1.0  0.0
3           4  0.0  0.0
4           5  0.0  0.0
5           6  0.0  1.0
  Box_deatils    1    5
0           1  0.0  0.0
1           2  1.0  0.0
2           3  1.0  0.0
3           4  0.0  0.0
4           5  0.0  0.0
5           6  0.0  1.0
 

行删除没有发生,只有列正在发生

请帮助找出下面的错误。

Soln=[[0. 0. 0. 0. 0. 0.]
     [1. 0. 0. 0. 0. 0.]
     [1. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 1. 0.]]
    
    result=pd.DataFrame(Soln)

Final_result=result.loc[:,(result !=0).any(axis=0)]

print(Final_result)

Final_result=Final_result.loc[(Final_result !=0).any(axis=1)]

print(Final_result)
4

2 回答 2

0

小心.locan .iloc,它们不是一回事,更多信息在这里:loc and iloc in Pandas

我会使用“loc/iloc”方法或.where我个人觉得更容易的方法:

df= df.where((df['column1'] != 0) & (df['column2'] != 0))

这在很多情况下对我有用。

于 2020-08-31T10:58:05.323 回答
0

据我了解,用零填充的行实际上是所有“数字命名”列(在您的情况下为15)只有零的行(Box_deatils(或Box_details)列中的值无关紧要)。

为此,您可以运行:

df.drop(df.index[df.iloc[:, 1:].eq(0).all(axis=1)], inplace=True)

df现在包含:

   Box_deatils    1    5
1            2  1.0  0.0
2            3  1.0  0.0
5            6  0.0  1.0
于 2020-08-31T12:44:46.317 回答