Python Forum
How to drop column in pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to drop column in pandas
#1
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.
Reply
#2
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.
Reply
#3
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 962 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  pandas column percentile nuncio 7 2,383 Aug-10-2022, 04:41 AM
Last Post: nuncio
  pandas: Compute the % of the unique values in a column JaneTan 1 1,756 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  Pandas Data frame column condition check based on length of the value aditi06 1 2,655 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  How to move each team row to a new column. Pandas vladiwnl 0 1,694 Jun-13-2021, 08:10 AM
Last Post: vladiwnl
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 3,268 May-09-2021, 06:46 PM
Last Post: Pit292
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,915 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Pandas: summing columns conditional on the column labels ddd2332 0 2,075 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Pandas DataFrame and unmatched column sritsv19 0 2,989 Jul-07-2020, 12:52 PM
Last Post: sritsv19
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 3,011 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020