Add another column to existing list - python

I'm starting to learn python and I'm trying to do an exercise where I have to save in a "rows" variable some stock data coming from a SQL query, like this:
rows = db.execute("SELECT * FROM user_quote WHERE user_id=:userid", userid=session["user_id"])
This will return 4 columns (id, user_id, symbol, name)
Then, for every row the query returns I'll get the last known price of that stock from an API, and I want to add that information to another column in my rows variable. Is there a way to do this? Should I use another approach?
Thanks for your time!

I'm not sure what type the rows variable is, but you can just add an additional column in the SELECT:
rows = db.execute("SELECT *, 0 NewCol FROM user_quote WHERE user_id=:userid", userid=session["user_id"])
Assuming rows is mutable, this will provide a placeholder for the new value.

Convert the rows tuple to a list, then you can use append() to add the price.
rows = list(rows)
rows.append(price)

Related

How can i keep original index when doing outer merge and dropping rows?

I have a big df (rates) that contains all information, then I have a second dataframe (aig_df) that contains a couple of rows of the first one.
I need to get a 3rd dataframe that is basically the big one (rates) without the rows on the second one (aig_df), but I need to keep the corresponding indices of the rows that results of rates without aig_df.
With the code I have now, I can get the 3rd dataframe with all the information needed but with int index and I need the index corresponding to each row (Index = Stock Ticker).
rates = pd.read_sql("SELECT Ticker, Carrier, Product, Name, CDSC,StrategyTerm,ParRate,Spread,Fee,Cap FROM ProductRates ", conn).set_index('Ticker')
aig_df = rates.query('Product == "X5 Advantage AnnuitySM"')
competitors_df = pd.merge(rates, aig_df[['Carrier', 'Product', 'Name','CDSC','StrategyTerm','ParRate','Spread','Fee','Cap']],indicator=True,
how='outer').query('_merge=="left_only"').drop('_merge',axis=1)
¿Is there any way to do what I need?
Thanks for your attention
In your specific case, you don't need a merge to do what you want:
result = rates[rates["Product"] != "X5 Advantage AnnuitySM"]

Python select 5last value of an excel column

I wonder if it is possible to retrieve the last 5 values of an excel column instead of all the values in that same column.
Currently I am able to select all the data in the column with the following piece of code:
var= pd.read_excel("Path/MyFile.xlsx",'MS3',skiprows=15)
xDate = list(var['Date'])
Is there a way to retrieve the last 5 values in this column?
yes you can use tail like head
var.tail(5)
you can simply go for this:
xDate[-5:]

How to get all the values in a column from a database in python (SQLAlchemy)

I have a table called draws. All I want to do is get the column 'user_id' as an array, in order from the 'id' column (top to bottom).
The ID column just numbers each row.
I've written this:
user_id = Draw.query.order_by(desc('id')).all()
This query gets every column though right? I just want the information in the 'user_id' column
user_id = Draw.query.with_entities(Draw.user_id).order_by(desc(Draw.id)).all()
With entities explanation from the docs.
Alternatively you can just do a list comprehension on the query you already have -- something like:
user_ids = [d.user_id for d in user_id]
Try this!
from sqlalchemy import desc
db.session.query(Draw.user_id).order_by(desc(Draw.id)).all()

Get row values of CSV file having particular value using python

I have multiple
I want to get rows of Name.
I know how to get by index using dataframe but I want to get using row name as index might change.
like
(row=="Name" ) or (row== "name")
output be like :
Thanks in advance!
If you want the name column name = df['Name']

Python sqlite how to grab variable of selected value

I have this definition in my python script
def read_from_db():
c.execute("SELECT title FROM book WHERE title LIKE 'Huckle%' ")
for row in c.fetchall():
print row
title = row
I wanted to save the value of the title into another variable. but it seems that it doesn't work like that?
I think you might just want to do title = row[0]. Row contains all column values of the particular row in the result set. Even if you select a single column you still need to index to get that value.

Categories