Answer the question
In order to leave comments, you need to log in
How do I group all the text that is there where the same id?
I have a database where there are products, also next to each product there is a telegram user id who actually bought this product, but when outputting, there may be such a picture that all products that 1 user bought do not go in order,
for example, this is how I need to change my code so that there is something like this EXAMPLE: Timur) hevevs Man, Yo-Yo, banana And here is the phone number and the same with another user EXAMPLE: Yo-yo, Horddg Orange 2pcs, Banana Apple 2pcs, Phone number I want everything to be in order and the same words and names were not repeated except for the products here is my code but it does not work the way I want
con = sqlite3.connect("telebot00.db")
cur = con.cursor()
count_back = 0
order = 'Orders'
back_result = cur.execute(f"""SELECT * FROM {order}""").fetchall()
for back_elem in back_result:
user_id_bay = back_result[1]
count_back = count_back + 1
back = back + str(count_back) + ') '
back = back + back_elem[2]
back = back + ' ,'
back = back + back_elem[3]
back = back + ' ,'
back = back + str(back_elem[4])
Answer the question
In order to leave comments, you need to log in
IMHO, if you work with the full database in the script, i.e. do SELECT * FROM
then the result can be immediately converted to pandas in pandas.DataFrame, there is a function for reading from sql.
You can group by unique combinations - user_id + product, it is assumed that the user data for one user_id is always the same.
pandas.df1.groupby(['user_id','product']).count()
Or, if via SQL, make queries one by one by user_id and already do groupby and count in them
SELECT name, tel, product, COUNT(product)
FROM order
WHERE user_id=....
GROUP BY name, product
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question