I´m accessing a Microsoft SQL Server database with pyodbc in Python and I have many tables regarding states and years. I´m trying to create a pandas.DataFrame with all of them, but I don't know how to create a function and still create columns specifying YEAR and STATE for each of these states and years (I'm using NY2000 as an example). How should I build that function or "if loop"? Sorry for the lack of clarity, it's my first post here :/
tables = tuple([NY2000DX,NY2001DX,NY2002DX,AL2000DX,AL2001DX,AL2002DX,MA2000DX,MA2001DX,MA2002DX])
jobs = tuple([55,120])
query = """ SELECT
ID,
Job_ID,
FROM {}
WHERE Job_ID IN {}
""".format(tables,jobs)
NY2000 = pd.read_sql(query, server)
NY2000["State"] = NY
NY2000["Year"] = 2000
My desirable result would be a DF with the information from all tables with columns specifing State and Year. Like:
| Year | State | ID | Job_ID |
|---|---|---|---|
| 2000 | NY | 13 | 55 |
| 2001 | NY | 20 | 55 |
| 2002 | NY | 25 | 55 |
| 2000 | AL | 15 | 120 |
| 2001 | AL | 60 | 120 |
| 2002 | AL | 45 | 120 |
| ------------ | ------- | -------- | ---------- |
Thanks for the support :)