2

Specifically, I used pandasql to query a pandas dataframe like this:

dataset = "SELECT assetName, universe,volume,returnsClosePrevRaw1 AS Close FROM market ORDER BY assetName DESC LIMIT 5"

print(ps.sqldf(dataset, locals())) 

The class of "dataset" is str.

I will like to save "dataset" as a csv file to pandas dataframe

Here is a print screen:

enter image description here

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
ad1
  • 31
  • 3
  • have you tried `data = pd.read_sql_query(dataset, engine); data.to_csv('file.csv')` as described in this answer [SQL query output to .csv](https://stackoverflow.com/a/52448387/1248974) – chickity china chinese chicken Dec 12 '18 at 00:20
  • I have been to find a solution by importing pysqldf from pandasql and using the code: – ad1 Dec 12 '18 at 07:07
  • great to hear you solved it. – chickity china chinese chicken Dec 12 '18 at 07:13
  • I have been to find a solution by importing pysqldf from pandasql and using the codes: pysqldf = lambda q: sqldf(q, globals()) pd.DataFrame(pysqldf("SELECT assetName, universe,volume,returnsClosePrevRaw1 AS Close FROM market ORDER BY assetName DESC;")).to_csv("data1.csv") – ad1 Dec 12 '18 at 07:17
  • Please [put your solution in an answer below](https://stackoverflow.com/help/self-answer) and [accept your answer](http://blog.stackoverflow.com/2009/01/accept-your-own-answers/) so we can close this question and make it available to future readers. – chickity china chinese chicken Dec 12 '18 at 07:21

1 Answers1

1

I have been able to find a solution by importing pysqldf from pandasql and using the codes:

p

ysqldf = lambda q: sqldf(q, globals()) 

pd.DataFrame(pysqldf("SELECT assetName, universe,volume,returnsClosePrevRaw1 AS Close FROM market ORDER BY assetName DESC;")).to_csv("data1.csv")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ad1
  • 31
  • 3