I want to get an individual row from the QueryJob in BQ. My query: select count(*) from ... returns a single row & I want to read the count value which is its first column. So if I can get the first row then I can do row[0] for the first column. I can iterate: row in queryJob but since I require only the first row this seems unneccesary.
Below is what I've tried:
row = self.client.query(count_query)
count = row.result()[0]
This gives an error:
'QueryJob' object is not subscriptable"
How can I get individual rows from queryJob by the row index?
Just do:
row = self.client.query(count_query)
result = row.result().total_rows
This will give the count from the query
you can use to_dataframe():
result = self.client.query(count_query).to_dataframe()
#if you want to result as a integer:
result = self.client.query(count_query).to_dataframe()['first_column_name'].iat[0]
Related
I have a dataframe test. My goal is to search in the column t1 for specific strings, and if it matches exactly a specific string, put that string in the next column over called t1_selected. Only thing is, I can't get iterrows() to go over the entire dataframe, and to report results in respective rows.
for index, row in test.iterrows():
if any(['ABCD_T1w_MPR_vNav_passive' in row['t1']]):
#x = ast.literal_eval(row['t1'])
test.loc[i, 't1_selected'] = str(['ABCD_T1w_MPR_vNav_passive'])
I am only trying to get ABCD_T1w_MPR_vNav_passive to be in the 4th row under the t1_selected, while all the other rows will have not found. The first entry in t1_selected is from the last row under t1 which I didn't include in the screenshot because the dataframe has over 200 rows.
I tried to initialize an empty list to append output of
import ast
x = ast.literal_eval(row['t1'])
to see if I can put x in there, but the same issue occurred.
Is there anything I am missing?
for index, row in test.iterrows():
if any(['ABCD_T1w_MPR_vNav_passive' in row['t1']]):
#x = ast.literal_eval(row['t1'])
test.loc[index, 't1_selected'] = str(['ABCD_T1w_MPR_vNav_passive'])
Where index is the row its written to. With i it was not changing
I am working with a QTableView and trying to retrieve values from the selected row(s). At other times I will be working with mulitiple rows using:
self.tableView5.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
The code below works, but only when the first row is selected. However, it shows:
identity[row].append(str(self.table_model5.data(index)))
IndexError: list index out of range
when another row is clicked.
names = []
emails = []
identity = []
data = sorted(set(index.row() for index in self.tableView5.selectionModel().selectedRows()))
for row in data:
identity.append([])
for column in range(0,2):
index = self.table_model5.index(row, column)
identity[row].append(str(self.table_model5.data(index)))
for item in identity:
names.append(item[0])
emails.append(item[1])
for name, recipient in zip(names, emails):
print(name, recipient)
The problem here is caused by the convoluted method you are using to populate the lists of values. I would suggest the following simplification:
names = []
emails = []
identity = []
for index in sorted(self.tableView5.selectionModel().selectedRows()):
row = index.row()
name = self.table_model5.data(self.table_model5.index(row, 0))
email = self.table_model5.data(self.table_model5.index(row, 1))
identity.append((name, email))
names.append(name)
emails.append(email)
Note that there is no need to use set, because selectedRows only returns one index per selected row.
It's identity[row] that throws this exceptions. Imagine you selected from the table rows 2 and 3. Then in the first iteration of your for row in data: loop the value of row will be 2 while your identity list will be one element long at that time.
Debug and fix your logic.
I have a SQL query in python like so to retrieve the first, fourth, and fifth column elements if it exists
cur2.execute('SELECT * FROM duplicates where TITLE=?', [post_title])
sql2.commit()
if cur2.fetchone():
repost_permalink = cur.fetchone()[0]
repost_title = cur.fetchone()[3]
repost_submitter = cur.fetchone()[4]
Forr some reason I keep getting the error:
repost_permalink = cur.fetchone()[0]
TypeError: 'NoneType' object has no attribute '__getitem__'
Am I accessing the element incorrectly?
Every time you call fetchone(), it fetches another row. So you are fetching four different rows in your code, because you call fetchone four times. If there aren't that many rows in the result set, some of them will be None.
If you want to get parts of a single row, store the row and then access it:
row = cur2.fetchone()
if row:
repost_permalink = row[0]
repost_title = row[3]
repost_submitter = row[4]
To sum it up. I have data in a row of a db (MySQL). This data are used in the script to calculate a value. A for loop is used to calculate this value for each row in the table.
The Problem: The results stored in the variable meant have to be updated in a different column of the same row.
I tried to include the update command in the for loop but I get an error (see below). If I do not include the update-command in the for loop I just get the result of the last row but for all rows!
The script:
for row in rows:
for col in row:
x = map(str.strip,col.lower().split())
st = map(lambda wo: abo.get(wo, 0), x)
meant = numpy.meant(st)
cur.execute("""UPDATE table_x SET result = %s Where text = %s""",(meant, rows))
Unfortunately I am getting this error:
Programming Error: (1064, 'You have an error in your SQL syntax; check the manual...
How can I update the column with each calculated value (meant) of that row?
As I see rows is a list ( or an iterable )
and in your query, you are binding the placeholder with rows ( instead of row? )
cur.execute("""UPDATE table_x SET result = %s Where text = %s""",(meant, rows)
That would most probably create a syntax error.
I want to insert my data which is stored in a sqlite table, to a QTableWidget. I use two for loop to find the data and index. after each iteration I print the data in console and it is OK but when it displays the table widget there is only the first row and the last row filled with the data.
Any idea to solve this problem?
It's obvious that tblTable is a QTableWidget!
Here is this part of the code:
cursor.execute('''SELECT * FROM MyTable''')
for index , form in enumerate(cursor.fetchall()):
i = 0
for item in form:
print(str(item))
self.tblTable.setItem(index, i, QtGui.QTableWidgetItem(str(item)))
i = i + 1
self.tblTable.insertRow(1)
You keep inserting your new row at position 1. What happens is that the previously entered data is then moved up one row, at which point you overwrite that data in the next loop.
So, first iteration everything is inserted in row 0, you add a row at index 1. Then you update row 1 with data, and insert another row at position 1, making the previously modified row move to row 2. Next loop, you overwrite the data on row 2, insert another empty row at position 1, moving the row with data to position 3 and you overwrite it again, etc., etc.
Set the row-count to 0 at the start, and insert rows as you need them before you insert your column data:
cursor.execute('''SELECT * FROM MyTable''')
self.tblTable.setRowCount(0)
for row, form in enumerate(cursor):
self.tblTable.insertRow(row)
for column, item in enumerate(form):
print(str(item))
self.tblTable.setItem(row, column, QtGui.QTableWidgetItem(str(item)))
I am not that familiar with the QtTableWidget, it could be that continually adding rows in not going to perform as well as setting the number of rows up front.
If sqlite's cursor.rowcount attribute is properly updated on your query (it not always is), you'd be better off calling .setRowCount with that value:
cursor.execute('''SELECT * FROM MyTable''')
self.tblTable.setRowCount(cursor.rowcount)
for row, form in enumerate(cursor):
for column, item in enumerate(form):
self.tblTable.setItem(row, column, QtGui.QTableWidgetItem(str(item)))
If the .rowcount value is not available (set to 1 or similar), perhaps first asking the database for the number of rows can help:
rowcount = cursor.execute('''SELECT COUNT(*) FROM MyTable''').fetchone()[0]
self.tblTable.setRowCount(rowcount)
cursor.execute('''SELECT * FROM MyTable''')
for row, form in enumerate(cursor):
for column, item in enumerate(form):
self.tblTable.setItem(row, column, QtGui.QTableWidgetItem(str(item)))
In all examples above, I also renamed you variables to something a little closer to their use, and used enumerate on the item loop as well. Last, but not least, the cursor can act as an iterator, meaning you can loop over rows directly without calling .fetchall() and rows will be fetched as needed.