alramdein
alramdein

Reputation: 901

Pandas : Usecols do not match columns, columns expected but not found

I got an error :

ValueError: Usecols do not match columns, columns expected but not found: ['Search Query']

No matter what the column name is, it still didn't work.

Here is my code :

if __name__ == '__main__':
    count = 0
    conn = MongoClient()
    db = conn.dbTweetsTA
    twit = []
    data_query = []
    collectionList = []
    dataB = pd.read_csv('listQuery.csv', usecols=['Search Query'])
    query_list = dataB['Search Query'].tolist()
    dataB.info()
    print(dataB)

Here is my csv :

enter image description here

Printed csv :

enter image description here

The separator is \t, probably that is the problem, but how do I get the column name only ?

Upvotes: 2

Views: 7092

Answers (1)

Andy L.
Andy L.

Reputation: 25239

your separator is \t which is 2 chars. read_csv interprets it as regex. You need escape the \ and specify raw string. It will use python engine on regex, so just specify it to avoid warning

dataB = pd.read_csv('listQuery.csv', sep=r'\\t', 
                     usecols=['Search Query'], engine='python')

Upvotes: 2

Related Questions