Search code examples
pythonmysqllisttuplesta-lib

from MySQL to CSV format


I want to use data from a mySQL select query for working with ta-lib. If I import data from a CSV file with

df = pd.read_csv('file.csv', sep=',')

then I get the desired result

          time     open     high      low    close
0   1573056000  9334.92  9334.92  9285.00  9319.64
1   1573059600  9319.63  9322.26  9250.01  9294.04
2   1573063200  9294.03  9311.21  9265.50  9304.15
3   1573066800  9303.98  9340.00  9292.23  9293.50
4   1573070400  9292.96  9307.17  9272.52  9293.01
5   1573074000  9293.01  9321.58  9293.00  9320.13
6   1573077600  9320.11  9350.97  9315.15  9337.15
7   1573081200  9338.37  9363.58  9335.00  9339.05
8   1573084800  9339.16  9367.99  9322.68  9340.83
9   1573088400  9341.24  9375.00  9301.93  9323.16
10  1573092000  9323.11  9327.45  9275.12  9297.06

but when I execute the MySQL query I get tuples within a list

[(1573056000, 9334.92, 9334.92, 9285.0, 9319.64), (1573059600, 9319.63, 9322.26, 9250.01, 9294.04), (1573063200, 9294.03, 9311.21, 9265.5, 9304.15)]

how can I modify the MySQL output to the same format when importing from a CSV?


Solution

  • The comments are correct. To be more explicit, your code would look something like

    sql_statement = "SELECT * FROM TABLE "
    df = pd.read_sql(sql_statement, mysql_connection)
    

    That would create a dataframe, just like your desired output.