Похожие чаты

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

20 просмотров

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

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

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

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта