3

I want get a db into pandas df in Python. I use a following code:

self.cursor = self.connection.cursor()
query = """
        SELECT * FROM `an_visit` AS `visit`                 
        JOIN `an_ip` AS `ip`  ON  (`visit`.`ip_id` = `ip`.`ip_id`)
        JOIN `an_useragent` AS `useragent` ON (`visit`.`useragent_id` = `useragent`.`useragent_id`)                 
        JOIN `an_pageview` AS `pageview`  ON (`visit`.`visit_id` = `pageview`.`visit_id`)       
        WHERE `visit`.`visit_id` BETWEEN  %s AND %s
        """
self.cursor.execute(query, (start_id, end_id))

df = pd.DataFrame(self.cursor.fetchall())

This code works, but I want to get column names as well. I tried this question MySQL: Get column name or alias from query

but this did not work:

fields = map(lambda x: x[0], self.cursor.description)
result = [dict(zip(fields, row)) for row in self.cursor.fetchall()]

How can I get column names from db into df? Thanks

5
  • Can you explain a bit more what do you mean by "get column names"? What is your desired output? Commented Dec 27, 2021 at 9:36
  • @sophocles now my df has names caled 1, 2, 3, ..., 25. I would like to have the same names, asi in the DB, which is ip_id, useragent_i etc. Commented Dec 27, 2021 at 9:40
  • when you say that the above code is not working, is it producing an error or not giving you the expected output? Commented Dec 27, 2021 at 10:25
  • @Jayvee I already solved it, see my answer, thank you for your effort! Commented Dec 27, 2021 at 10:31
  • 1
    Cool, glad to hear that Commented Dec 27, 2021 at 10:40

4 Answers 4

5

The easy way to include column names within recordset is to set dictionary=True as following:

self.cursor = self.connection.cursor(dictionary=True)

Then, all of fetch(), fetchall() and fetchone() are return dictionary with column name and data

check out links: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursordict.html

https://mariadb-corporation.github.io/mariadb-connector-python/connection.html

Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU! Why does this not have more likes. You saved me, thanks!
4

What work to me is:

field_names = [i[0] for i in self.cursor.description ]

Comments

0

the best practice to list all the columns in the database is to execute this query form the connection cursor

SELECT TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE  TABLE_SCHEMA='<schema>' AND TABLE_NAME = '<table_name>'

1 Comment

ok, can you write it so it pass to my example above? I don`t know how to modify it to my case.
0

There is a column_names properties in MySql cursor that you can use:

row = dict(zip(self.cursor.column_names, self.cursor.fetchone()))

https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-column-names.html

5 Comments

I tried, but AttributeError: 'Cursor' object has no attribute 'column_names'
are you connecting to a mysql DB?
yes, using import pymysql as mysql and self.connection = mysql.connect()
that's odd, are you qualifying it with "self" , I've updated the answer.
yes, I do it with self within the method.. Same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.