Python Forum
How to drop column in pandas - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Data Science (https://python-forum.io/Forum-Data-Science)
+--- Thread: How to drop column in pandas (/Thread-How-to-drop-column-in-pandas)



How to drop column in pandas - SriMekala - Aug-26-2019

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.


RE: How to drop column in pandas - ThomasL - Aug-26-2019

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.


RE: How to drop column in pandas - perfringo - Aug-26-2019

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.


RE: How to drop column in pandas - snippsat - Aug-26-2019

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