Bottom Page

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.
Quote
#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.
Quote
#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.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  itertuples, new column, datetime, pandas karlito 6 347 Nov-29-2019, 11:07 AM
Last Post: karlito
  sort values of a column pandas karlito 2 370 Oct-22-2019, 06:11 AM
Last Post: karlito
  Dropping a column from pandas dataframe marco_ita 6 1,703 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 335 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  How to use the excel filename as a value to populate new column, using Pandas? Dequanharrison 5 776 Jun-26-2019, 11:11 PM
Last Post: Dequanharrison
  pandas change row value an existing column with conditionals Gigux 1 505 Jun-22-2019, 08:04 PM
Last Post: Gigux
  How to delete column if entire column values are "nan" Sri 4 749 Apr-13-2019, 12:16 PM
Last Post: Sri
  Drop a row if it contains a certain value in pandas deltanov 4 843 Nov-10-2018, 05:19 PM
Last Post: Daredevil
  Text to column pandas ms5573 0 684 Aug-25-2018, 08:18 PM
Last Post: ms5573
  Splitting values in column in a pandas dataframe based on a condition hey_arnold 1 1,769 Jul-24-2018, 02:18 PM
Last Post: hey_arnold

Forum Jump:


Users browsing this thread: 1 Guest(s)