Похожие чаты

Hello friends I want to do groupby based on city_name, but

in such a way that the sum of columns A and B show the frequency of each city.
If I want to explain better, it goes like this: Suppose each city has populations A and B. So the population of a city is equal to: A + B
Now I want to do groupby based on the population of each city and sort according to the population of the city in the output

my table:

city_name ║ A ║ B ║
city1 ║ 2 ║ 6 ║
city2 ║ 5 ║ 4 ║
city1 ║ 6 ║ 3 ║
city2 ║ 1 ║ 9 ║

can you help me please?

12 ответов

8 просмотров

Idk much sql so this might be wrong but is the following not something that you want? select city_name, sum(a + b) as population, from my_table group by population order by population desc

Sajjad-Najafi Автор вопроса
A X
Idk much sql so this might be wrong but is the fol...

thank you for replying yes its something like that you said but im in python and pandas library

Sajjad Najafi
thank you for replying yes its something like that...

I don't know python or pandas library either but it seems that the pandas library can take a sql statement as string. You can try passing the above to that. The name being: pd.read_sql_query if you're referring to the pandas library as pd variable. You get a connection to the database, and pass the sql statement as string and the connection to the above function.

Sajjad-Najafi Автор вопроса
Sajjad Najafi
ok , thanx 🙏🙏🙏

Let us know what the proper SQL to your question looks like once you get it working. That'd be informative.

Sajjad-Najafi Автор вопроса
A X
Let us know what the proper SQL to your question l...

I have csv dataframe for this work and we should use pandas groupby for solving i think i can solving this with your first helping: select city_name, sum(a + b) as population, from my_table group by population order by population desc i hope ✔️

Sajjad Najafi
I have csv dataframe for this work and we should ...

Makes sense. A seemingly helpful link then: https://datatofish.com/sql-to-pandas-dataframe/

Sajjad-Najafi Автор вопроса
Sajjad-Najafi Автор вопроса
A X
Makes sense. A seemingly helpful link then: https:...

I did this: # sum of A, B and store in dataframe as new column: mytable['total_AB']=mytable['A']+mytable['B'] then: mytable.groupby('city_name').sum()['total_AB'].nlargest(5)

Sajjad-Najafi Автор вопроса
A X
I see.

thank you for helping again

Похожие вопросы

Обсуждают сегодня

А еще в перле можно уже @arr1 + @arr2?
Sergei Zhmylove
53
Подскажите, где смотреть результат выполнения программы? Код: ;.686 ;Система команд процессора 686 ;.MODEL FLAT,stdcall ;Модель памяти плоская, станда...
Егор Анелькин
5
я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
I arrived here after a Chico Crypto show highlighted the project & the Team - the fact that the Team had a long history of successfully working with household names gave me e...
Banter is Bullish
1
Привет всем. появился вопрос. Разрабатываю сайт, в данный момент он запущен. Хостинг beget. Добавляю на сайт яндекс метрику с помощью полей client-settings (взято отсюда http...
Andrew
2
;.686 ;Система команд процессора 686 ;.MODEL FLAT,stdcall ;Модель памяти плоская, стандартный ;вызов процедуры ;option casemap:no...
Егор Анелькин
1
Почему у макбука экран не сенсорный?
✨ Anatolii Cherednichenko
28
почому оно не работает?
Vi Chapmann Chapmann
19
It's unfortunate that the "Then why are you here" or "lol you must've bought high" mantras always come up. I already tried bringing up this subject in the price channel but si...
Leroy Gerrits
4
Так а кто может спарсить всех участников чата? Идишники
Magic
17
Карта сайта