1

Is there an efficient way to drop n number of columns from a pandas df. For example, if you don't want anything past a certain column can you incorporate a function df.drop > n

For the df below I want to drop everything past column 2. I doing this via.

import pandas as pd

d = ({
    '1' : ['X','Y'],  
    '2' : ['A','B'], 
    '3' : ['C','D'],   
    '4' : ['A','B'], 
    '5' : ['C','D'], 
     })

df = pd.DataFrame(data=d)

df = df.drop(['3','4','5'], axis = 1)

But if there are more or less than 5 columns I would have to manually add or subtract the column values. If I'm constantly doing this it becomes cumbersome.

2
  • 2
    df=df.iloc[:,:4], will keep colummns, 0,1,2,3 Commented Aug 22, 2018 at 4:22
  • 1
    df.iloc[:,:n] will drop everything after n columns. If the number of columns is less than n then no error will occur. Commented Aug 22, 2018 at 4:26

1 Answer 1

1

you can use df.iloc,

df=df.iloc[:,:3] #sliced based on indices. will keeps columns 0 to 2 (indices).
(or)
df=df.loc[:,:'3'] #sliced based on column names
print(df)
1   2   3
X   A   C
Y   B   D
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.