Kirill Vavilov
Kirill Vavilov

Reputation: 15

How to rename columns with a delimeter

Index(['id', 'asins', 'brand', 'categories', 'colors', 'count', 'dateAdded',
   'dateUpdated', 'descriptions', 'dimension', 'ean', 'features',
   'flavors', 'imageURLs', 'isbn', 'keys', 'manufacturer',
   'manufacturerNumber', 'merchants', 'name', 'prices.amountMin',
   'prices.amountMax', 'prices.availability', 'prices.color',
   'prices.condition', 'prices.count', 'prices.currency',
   'prices.dateAdded', 'prices.dateSeen', 'prices.flavor', 'prices.isSale',
   'prices.merchant', 'prices.offer', 'prices.returnPolicy',
   'prices.shipping', 'prices.size', 'prices.source', 'prices.sourceURLs',
   'prices.warranty', 'quantities', 'reviews', 'sizes', 'skus',
   'sourceURLs', 'upc', 'vin', 'websiteIDs', 'weight'],
  dtype='object')

I have columns with . in them, how do I rename them to have only the part after the dot?

I can rename them manually, but what is the better way to do this?

Upvotes: 0

Views: 25

Answers (1)

sjw
sjw

Reputation: 6543

You can split the strings on the . and keep only the last item, like so:

df.columns = [col.split('.')[-1] for col in df.columns]

Upvotes: 1

Related Questions