Python Forum

Full Version: How to drop column in pandas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I have below DataFrame,

Group Category Subject  AC1         AC2    AC3     BM1        BM2    NJ1        NJ2     NJ3  
Va    Vb       Vc       Datecode    Run1   Status  Datecode   Rank   Datecode   Power voltage
HY    JK       KL       2019-08-01   23    29      2019-08-12 0      2019-08-03  123   110   
HH    MK       VN       2019-08-09   28    45      2019-08-06 32     2019-08-13  125   220 


I want to delete columns with column name 'Datecode" in "zeroth row", I can not use df.drop
df.drop(['Datecode'], axis=1), but it does not work. Kindly help, how to do this.
please do print(df.columns) and copy paste output 1:1 here, so that we can see what names your columns have.
If possible do also print(df.head()) and copy paste output 1:1 here also.
One can check names of the columns by df.columns.values. It seems to me that df.drop is not working because 'Datecode' is not the column name.
Something like this should do it,boolean indexing with loc.
>>> import pandas as pd

>>> df = pd.read_clipboard()
>>> df
  Group Category Subject         AC1   AC2     AC3         BM1   BM2         NJ1    NJ2      NJ3
0    Va       Vb      Vc    Datecode  Run1  Status    Datecode  Rank    Datecode  Power  voltage
1    HY       JK      KL  2019-08-01    23      29  2019-08-12     0  2019-08-03    123      110
2    HH       MK      VN  2019-08-09    28      45  2019-08-06    32  2019-08-13    125      220


>>> mask = df.iloc[0].isin(['Datecode'])
>>> df.loc[:, ~mask]
>>> df
  Group Category Subject   AC2     AC3   BM2    NJ2      NJ3
0    Va       Vb      Vc  Run1  Status  Rank  Power  voltage
1    HY       JK      KL    23      29     0    123      110
2    HH       MK      VN    28      45    32    125      220