简体   繁体   中英

Iterating through rows in a data frame pandas

I am trying to build an app which generate SQL queries by fetching data from Excel files. I am trying to achieve it through Pandas, but the problem is that, I only got one row in return, but I have more than one rows in Excel.

Excel文件数据

Code:

from pandas import *
df1=pandas.read_excel("supermarkets.xlsx")
#SOURCE=df1
#ARGET="Aditya"
def SQL_DATAFRAME(SOURCE, TARGET):
    sql_texts = []
    for index, row in SOURCE.iterrows():
        sql_texts = 'INSERT INTO '+TARGET+' ('+ str(', '.join(SOURCE.columns))+ ') VALUES '+ str(tuple(row.values))
        return(''.join(sql_texts)+(";"))

print(SQL_DATAFRAME(df1,"Aditya"))

result:

INSERT INTO Aditya (ID, Address, City, State, Country, Supermarket Name, Number of Employees) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8);

It is merely a problem with your for loop. Try the following :

  sql_texts = []
 for index, row in SOURCE.iterrows():
    sql_texts. append('INSERT INTO '+TARGET+' ('+ str(', '.join(SOURCE.columns))+ ')   VALUES '+ str(tuple(row.values)))
 return(''.join(sql_texts)+(";"))

That way, on each iteration, you are appending your query string in the sql_texts list. When you exit the loop, you return the joined query string.

That being said, wouldn't pandas.to_sql do the trick ?

from sqlalchemy import create_engine
engine = create_engine('mydatabaseconnectiondetails')
SOURCE.to_sql(TARGET, engine, if_exists = 'append', index= False)

With a little configuration that should definitely work in your case - and would be faaaar more performing.

You have to get the return statement out of for block:

from pandas import *
df1=pandas.read_excel("supermarkets.xlsx")
#SOURCE=df1
#ARGET="Aditya"
def SQL_DATAFRAME(SOURCE, TARGET):
    sql_texts = []
    for index, row in SOURCE.iterrows():
        sql_texts = 'INSERT INTO '+TARGET+' ('+ str(', '.join(SOURCE.columns))+ ') VALUES '+ str(tuple(row.values))
    return(''.join(sql_texts)+(";"))

print(SQL_DATAFRAME(df1,"Aditya"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM