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?
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
thank you for replying yes its something like that you said but im in python and pandas library
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.
ok , thanx 🙏🙏🙏
Let us know what the proper SQL to your question looks like once you get it working. That'd be informative.
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 ✔️
Makes sense. A seemingly helpful link then: https://datatofish.com/sql-to-pandas-dataframe/
ok thank you ✅
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)
thank you for helping again
No issues. :)
Обсуждают сегодня