Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Length of rows
#1
Hi,

How can I determine number of a value occur in a column from an sql db?

con = pymysql.connect(host='localhost', user='root', password='*', database='prices')
            cur = con.cursor()
            cur.execute('select Var78 from prices ')
            rows = cur.fetchall()

            if rows != 0:
               self.totalrecord.set(len(rows))
               table.delete(*table.get_children())

            for row in rows:
                if row[78] == 'A':
                    table.insert("", END, values=row, tags='Albastru')
                    a = len(rows)
                    print(a)
It result:
7
7
7
7
where 7 is the total number of rows and it occur 4 times, but I need number 4 as result.
Thanks
Reply
#2
That is obvious. "Rows" is not changed so it will count the the same number of rows every time.
If you want to count the rows with "A" in position 78, you should count them. For example:
rows_with_a = 0
for row in rows:
    if row[78] == 'A':
        table.insert("", END, values=row, tags='Albastru')
        rows_with_a += 1
print(rows_with_a)
Obviously you show us a small part of your program and we cannot see what the "table" object is.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,624 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,105 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,097 Mar-02-2021, 01:54 AM
Last Post: Jeremy7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020