Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read all columns and change name of a column in mysql

I have a table in Mysql. The table has more than 200 columns. I want to read all the columns, however, I have to read one of the column (A) as a specific name. Now, I have to use two lines of code and it take times.

df1 = pd.read_sql("""SELECT * FROM mytable""", db1)
df2 = pd.read_sql("""SELECT A as specific_name FROM mytable""", db1)

I want to ask that is anyway to use only one line of code?
Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try something like this:

df = pd.read_sql("""SELECT T.*, T.A as specific_name FROM mytable T""", db1)

It should give all columns of my table and then added specific_name column at the end.

Update:

As a side note – if you have really big table (rows-wise) – you can just select all columns (don’t query specific_name), and then rename column A with df.rename(columns={"A": "specific_name"}). Saves traffic and I would guess more elegant.

df = pd.read_sql("""SELECT T.* FROM mytable T""", db1)
df.rename(columns={"A": "specific_name"})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading