Похожие чаты

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 ответов

11 просмотров

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

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

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

Random thoughts. Need mode opinions. def main(): conn = psycopg.connect(user="postgres", password="example", host="localhost", port=5432, dbname="postgres") with conn...
Artem
13
А как старый хаскел с новым стыковать ? потому как тут работает https://play.haskell.org/saved/C3xpMzcd, а вот тут https://stepik.org/lesson/7602/step/9?unit=1473 нет ошибка C...
Fedor
131
Народ, кто шарит в расширенных разделах (EBR/EPR) на дисках с разметками MBR? Везде пишут (в вики рус/англ) в частности + другие источники смотрел, что первый сектор расширенн...
Eugene Krasnikov (ᴊɪɴ x)
1
что насчет пагинга? на осдеве непонятно(
Vi Chapmann 🪙
26
Moneor is doing well - im happy to have bought some when it was lower. What do you guys think about Pirate Chain? Can it be an alternative to Monero? Im into privacy coins and...
Alex
12
Guys what is ur real (no jokes) expectation of link price during this cycle?
Hobbyist
28
hey everyone I'm not able to input come characters in my flatpak apps, they get ignored by the application. all other non-flatpak apps are fine with these characters. I search...
elo
20
Вопрос я правильно понимаю что в коде newtype ArrowMap k v = ArrowMap { getArrowMap :: k -> Maybe v } getArrowMap есть функция типа k -> Maybe v, если да, то не понимаю задач...
Fedor
64
>>> Failed to install app-alternatives/awk-4, Log file: >>> '/var/tmp/portage/app-alternatives/awk-4/temp/build.log' * Messages for package app-alternatives/awk-4: * Pa...
[cocoa] wizzy hell
1
Hey everyone! How's your weekend going so far?
Noah Noure
17
Карта сайта