Похожие чаты

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

17 просмотров

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

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

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

30500 за редактор? )
Владимир
47
any reference of this implementation?
BitBuddha
29
Ⓐrtto, [4/23/24 7:02 PM] Please explain more fully how it is not working exactly, and what are the steps you are taking, and what error messages come or what happens. Ⓐrtto, ...
Ezza Kezza
2
sounds like people have lost their kaspa on tradeogre... does this mean tradeogre not trustworthy?
Ezza Kezza
15
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Недавно Google Project Zero нашёл багу в SQLite с помощью LLM, о чём достаточно было шумно в определённых интернетах, которые сопровождались рассказами, что скоро всех "ибешни...
Alex Sherbakov
5
So much speculation in the last week. So much volatility in price. This is because Hedera has a GC that isn't using the network it's governing. Why aren't people asking why a...
Summit Seeker R
9
Anyone else having this error when trying to make transactions?
Datzel
11
Question: How viable is it to use Anvil as the backend infrastructure for managing a TradFi portfolio, while integrating Flexa for instant liquidity and payment solutions? Cou...
Kevin
2
вы делали что-то подобное и как? может есть либы готовые? увидел картинку нокода, где всё линиями соединено и стало интересно попробовать то же в ddl на lua сделать. решил с ч...
Victor
8
Карта сайта