Home > Back-end >  Drop column and line in pivot table
Drop column and line in pivot table

Time:09-17

Having a pivot table as below:

imgg

is it possible that i can drop a column and row? I need to drop the column and line 'C'?

Is it possible that i can drop the None value? None is a <class 'NoneType'>

IMG

CodePudding user response:

Sure, use DataFrame.drop:

np.random.seed(1609)

df = pd.DataFrame(np.random.randint(10, size=(4,4)), 
                  index=list('ABCD'), 
                  columns=list('ABCD'))
print (df)
   A  B  C  D
A  5  3  6  9
B  5  4  0  4
C  0  7  5  9
D  0  6  3  3

df = df.drop(index='C', columns='C')

print (df)
   A  B  D
A  5  3  9
B  5  4  4
D  0  6  3

EDIT: If None is Nonetype for me working:

df = pd.DataFrame(np.random.randint(10, size=(5,5)), 
                  index=list('ABCD')   [None], 
                  columns=list('ABCD')   [None])
print (df)
     A  B  C  D  NaN
A    5  3  6  9    5
B    4  0  4  0    7
C    5  9  0  6    3
D    3  0  0  8    4
NaN  3  5  3  3    1

df = df.drop(index=[None], columns=[None])

print (df)
   A  B  C  D
A  5  3  6  9
B  4  0  4  0
C  5  9  0  6
D  3  0  0  8
    
  • Related