1

I have a list of rows imported from SQL (using PyMySQL), based on user inputs. Sometimes these returns can number over 5000 rows (these are stored in a 2D array, with at least 4 columns). This is not practical for a user to be able to read when printed out on the screen.

I have implemented a limiter, which will return the first X number of rows, based on a user's input. However, I would like to return a sample of the rows, instead of the first X.

i.e. if a user selected 100 rows, instead of getting the first 100 rows, they would get a sample of size 100, made up of random rows in the array. Is there a way to do this?

The code I have so far is:

with conn:
   cursor = conn.cursor()
   cursor.execute(query, Pop_Lim)
   city = cursor.fetchall()

   if len(city) >= 50:
         print()
         print("This search will return ",len(city), "rows of data.")
         Ret_Lim = int(input("How many rows of data do you want to display? "))
         print()

         with conn:
            cursor = conn.cursor()
            cursor.execute(query, Pop_Lim)
            city = cursor.fetchmany(Ret_Lim)

            print("ID   :   CountryCode :   District    :   Population")
            for row in city:
               print(row["ID"], row["Name"]," :   ",row["CountryCode"]," :   ",row["District"]," :   ",row["Population"])     # insert spacers for legibility purposes
            print()
            print(Ret_Lim,"rows of data returned, as requested.")
Clauric
  • 1,826
  • 8
  • 29
  • 48

1 Answers1

1

I suggest you to use Pandas. https://pandas.pydata.org/

You could import your database into a pandas.DataFrame(), try with

import pandas as pd
df = pd.read_sql(query, connection,  params=(start_date, end_date))

then easily perform the operations you need.

In my opinion Pandas it's the best solution to handle huge dataframes and tables. You can easily get random rows as sample of the whole dataframe. get a look here:

Random row selection in Pandas dataframe

Hope it helps

Best regards

Michele Rava
  • 184
  • 4
  • 17