Delete data frame column if column name ends with some string, Python 3.6

11,017

You can filter by inverting (~) boolean mask for columns which not need delete with loc and str.endswith, also working str.contains with $ for match end of string:

cols = ['SectorName', 'Name Sector', 'ItemName', 'Item', 'Counterpart SectorName']
df = pd.DataFrame([range(5)], columns = cols)
print (df)
   SectorName  Name Sector  ItemName  Item  Counterpart SectorName
0           0            1         2     3                       4

print (~df.columns.str.endswith('Name'))
[False  True False  True False]

df1 = df.loc[:, ~df.columns.str.endswith('Name')]

df1 = df.loc[:, ~df.columns.str.contains('Name$')]

Or filter columns names first:

print (df.columns[~df.columns.str.endswith('Name')])
Index(['Sector', 'Item'], dtype='object')

df1 = df[df.columns[~df.columns.str.endswith('Name')]]

print (df1)
   Name Sector  Item
0            1     3
Share:
11,017

Related videos on Youtube

Learnings
Author by

Learnings

Updated on June 04, 2022

Comments

  • Learnings
    Learnings over 1 year

    I have a dataframe with below columns:

    SectorName', 'Sector', 'ItemName', 'Item', 'Counterpart SectorName', 'Counterpart Sector', 'Stocks and TransactionsName', 'Stocks and Transactions', 'Units', 'Scale', 'Frequency', 'Date', 'Value'
    

    How to delete column from df where column name ends with Name.

  • Learnings
    Learnings about 6 years
    thanks, I go for 2nd one.
  • Learnings
    Learnings about 6 years
    Can we give multiple, like Ends with 'Code', df1 = df[df.columns[~df.columns.str.endswith('Name', 'Code')]]
  • jezrael
    jezrael about 6 years
    You need chain conditions like df1 = df[df.columns[~(df.columns.str.endswith('Name') | df.columns.str.endswith('Code'))]]
  • jezrael
    jezrael about 6 years
    Or df1 = df.loc[:, ~df.columns.str.contains('Name$|Code$')]
  • Learnings
    Learnings about 6 years
    yes working fine... thanks again...
  • 3kstc
    3kstc over 5 years
    the ~ is a ! (not) so df[df.columns[~(df.columns.str.endswith('Name') will return all the columns that does not (!) have 'Name'
  • bernando_vialli
    bernando_vialli almost 4 years
    @jezrael, do you know, should the same code work if I am working with a pyspark dataframe instead of a pandas one?