Search code examples
pythonpython-3.xpandaspandasql

pandasql: How to select non english named column?


Dataframe has non english named columns, how to select such columns?

import pandasql as pdsql

pysql = lambda q: pdsql.sqldf(q, globals())
sqlquery = 'select ''Машина'', min(''Дата доставки'') from days_ans group by 1'
pysql(sqlquery)

Last row returns an error:

PandaSQLException: (sqlite3.OperationalError) near "доставки": syntax error [SQL: 'select Машина, min(Дата доставки) from days_ans group by 1']

PS Without specifying columns- works as should:

sqlquery = 'select * from days_ans limit 1'
sqlquery = 'select ''Партнер'' from days_ans limit 1'

I think the problem is related to the columns with spaces in it


Solution

  • Your single quotes '' concatenate and become nothing. Try to use double quotes at the both sides of a string instead:

    >>> sqlquery = 'select ''Машина'', min(''Дата доставки'') from days_ans group by 1'
    >>> sqlquery
    'select Машина, min(Дата доставки) from days_ans group by 1'
    >>> sqlquery = "select ''Машина'', min(''Дата доставки'') from days_ans group by 1"
    >>> sqlquery
    "select ''Машина'', min(''Дата доставки'') from days_ans group by 1"
    

    String literal concatenation