Numpy 或 Pandas 删除 全零行、全零列

使用numpy、pandas 实现删除全0的行或者列。

pandas删除全零行

from pandas import DataFrame
import numpy as np

df1 = DataFrame(np.arange(16).reshape((4,4)),index=['a','b','c','d'],columns=['one','two','three','four'])  # 创建一个dataframe
df1.loc['e'] = 0    # 优雅地增加一行全0
print(df1)
df1.ix[(df1==0).all(axis=1), :]    # 找到它
df2 = df1.ix[~(df1==0).all(axis=1), :]   # 删了它
print("======+=========")
print(df2)

'''
   one  two  three  four
a    0    1      2     3
b    4    5      6     7
c    8    9     10    11
d   12   13     14    15
e    0    0      0     0
======+=========
   one  two  three  four
a    0    1      2     3
b    4    5      6     7
c    8    9     10    11
d   12   13     14    15
'''

pandas删除全零列

from pandas import DataFrame
import numpy as np

df1 = DataFrame(np.arange(16).reshape((4,4)),index=['a','b','c','d'],columns=['one','two','three','four'])  # 创建一个dataframe
df1.loc[:,['four']] = 0    # 将一列全部设置为0
print(df1)
df1.ix[:, (df1==0).all(axis=0)]    # 找到它
df2 = df1.ix[:, ~(df1==0).all(axis=0)]   # 删了它
print("======+=========")
print(df2)

'''
   one  two  three  four
a    0    1      2     0
b    4    5      6     0
c    8    9     10     0
d   12   13     14     0
======+=========
   one  two  three
a    0    1      2
b    4    5      6
c    8    9     10
d   12   13     14
'''

numpy 删除全零行

import numpy as np

# 生成50个[0,10]间的随机数
data = np.random.choice(10, 50)

# 变形
data = data.reshape(10,5)

# 将这三行全设为0
data[3,:] = 0
data[7,:] = 0
data[8,:] = 0

print(data)
print('original data shape', data.shape)

print('----- after removing rows with only 0 -----')

# 删除全零行
# To remove all rows that contain only 0 we can use the following syntax
data = data[~np.all(data == 0, axis=1)]

print(data)
print('new data shape', data.shape)


'''
[[8 6 2 2 8]
 [5 3 0 2 5]
 [3 2 0 3 8]
 [0 0 0 0 0]
 [9 5 4 8 3]
 [9 7 8 6 4]
 [0 8 2 0 7]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [5 7 3 7 0]]
original data shape (10, 5)
----- after removing rows with only 0 -----
[[8 6 2 2 8]
 [5 3 0 2 5]
 [3 2 0 3 8]
 [9 5 4 8 3]
 [9 7 8 6 4]
 [0 8 2 0 7]
 [5 7 3 7 0]]
new data shape (7, 5)

'''

numpy 删除全零列

示例1 

 关键代码:

# 寻找并删除全零列
data= np.delete(data,np.where(~data.any(axis=0))[0], axis=1)
import numpy as np

# 生成50个[0,10]间的随机数
data = np.random.choice(10, 50)

# 变形
data = data.reshape(5,10)

# 将这三列全设为0
data[:,2] = 0
data[:,4] = 0
data[:,7] = 0

print(data)
print('original data shape', data.shape)

print('----- after removing columns with only 0 -----')

# 寻找全零列
print(np.where(~data.any(axis=0))[0])

# 删除全零列
# To remove all columns that contain only 0 we can also use the following syntax
data= np.delete(data,np.where(~data.any(axis=0))[0], axis=1)

print(data)
print('new data shape', data.shape)

'''
[[6 3 0 0 0 0 1 0 6 7]
 [2 8 0 2 0 6 2 0 2 5]
 [6 8 0 9 0 1 3 0 0 0]
 [3 1 0 6 0 3 7 0 2 5]
 [8 5 0 2 0 4 9 0 8 2]]
original data shape (5, 10)
----- after removing columns with only 0 -----
[2 4 7]
[[6 3 0 0 1 6 7]
 [2 8 2 6 2 2 5]
 [6 8 9 1 3 0 0]
 [3 1 6 3 7 2 5]
 [8 5 2 4 9 8 2]]
new data shape (5, 7)
'''

示例2

关键代码: 

# 删除全零列
mask = (array1 == 0).all(0)
column_indices = np.where(mask)[0]
array1 = array1[:,~mask]
import numpy as np

# 生成50个[0,10]间的随机数
array1 = np.random.choice(10, 50)

# 变形
array1 = array1.reshape(5,10)

# 将某些列设为0
array1[:,2]=0
array1[:,5]=0
array1[:,8]=0

print(array1)
print("raw  array", array1.shape)

# 删除全零列
mask = (array1 == 0).all(0)
column_indices = np.where(mask)[0]
array1 = array1[:,~mask]

print("================")
print("after array",array1.shape) 
print(array1)

'''
[[1 6 0 9 9 0 4 2 0 0]
 [8 1 0 3 6 0 1 9 0 6]
 [2 5 0 7 2 0 8 5 0 5]
 [8 2 0 3 2 0 9 1 0 6]
 [3 9 0 5 2 0 3 8 0 2]]
raw  array (5, 10)
================
after array (5, 7)
[[1 6 9 9 4 2 0]
 [8 1 3 6 1 9 6]
 [2 5 7 2 8 5 5]
 [8 2 3 2 9 1 6]
 [3 9 5 2 3 8 2]]
'''

示例3

关键代码:

# 删除全零列
idx = np.argwhere(np.all(array1[..., :] == 0, axis=0))
array1= np.delete(array1, idx, axis=1)
import numpy as np

# 生成50个[0,10]间的随机数
array1 = np.random.choice(10, 50)

# 变形
array1 = array1.reshape(5,10)

# 将某些列设为0
array1[:,2]=0
array1[:,5]=0
array1[:,8]=0

print(array1)
print("raw  array", array1.shape)

# 删除全零列
idx = np.argwhere(np.all(array1[..., :] == 0, axis=0))
array1= np.delete(array1, idx, axis=1)


print("================")
print("after array",array1.shape) 
print(array1)

'''
[[7 8 0 8 0 0 0 4 0 2]
 [9 4 0 2 4 0 2 4 0 1]
 [4 7 0 5 5 0 3 3 0 3]
 [5 0 0 9 8 0 3 2 0 7]
 [3 4 0 3 2 0 5 0 0 6]]
raw  array (5, 10)
================
after array (5, 7)
[[7 8 8 0 0 4 2]
 [9 4 2 4 2 4 1]
 [4 7 5 5 3 3 3]
 [5 0 9 8 3 2 7]
 [3 4 3 2 5 0 6]]
 '''

参考博客

How to remove array rows that contain only 0 in python (moonbooks.org)

Numpy(Pandas) 删除全为零的列_SongpingWang的博客-CSDN博客_pandas删除全为0的列

你可能感兴趣的:(Python常用代码,numpy,pandas,python)