简体   繁体   中英

How to strip white spaces in headers with Python and pandas

I am trying to load a tab deliminated file into pandas with python so I can perform different queries. Unfortunately the file formatting is nto great (it loads fine to Excel but I need to work with it programatically.

When I do:

>>> print(df.columns)

Index([u'Domain Name                                                      ',
       u'Current Bid ', u'Join By Date (ET)', u'Join By Date (PT)',
       u'Bidders ', u'Seller         ', u'TLD     ', u'Length',
       u'Words                                                                           ',
       u'Word Count',
       u'Categories                                                                                                      ',
       u'Hyphens ', u'Numbers ', u'Auction Type'],
      dtype='object')

How can I fix the file so simple df.query('TLD) == "value"') would work?

I get a new file every 5 days so I need to do it programatically.

Note - please have patience, I am new to scripting and Python

这应该有助于清理列名:

df.columns = [x.strip() for x in df.columns] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM