I would like to make a groupby and after a sort_values
Depuis_2013 = pd.DataFrame(aide1, columns=['Pays','Année','Aide_kg'])
Depuis_2013_aide = Depuis_2013.groupby (('Pays') ['Année']).sum().sort_values(by= "quantite_aide", ascending=False).head(7)
and I have this
<>:3: SyntaxWarning: str indices must be integers or slices, not str; perhaps you missed a comma? <>:3: SyntaxWarning: str indices must be integers or slices, not str; perhaps you missed a comma? C:\Users\HP\AppData\Local\Temp/ipykernel_5680/565621470.py:3: SyntaxWarning: str indices must be integers or slices, not str; perhaps you missed a comma? Depuis_2013_aide = Depuis_2013.groupby (('Pays') ['Année']).sum().sort_values(by= "quantite_aide", ascending=False).head(7)
TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_5680/565621470.py in 1 Depuis_2013 = pd.DataFrame(aide1, columns=['Pays','Année','Aide_kg']) 2 ----> 3 Depuis_2013_aide = Depuis_2013.groupby (('Pays') ['Année']).sum().sort_values(by= "quantite_aide", ascending=False).head(7)
TypeError: string indices must be integers
CodePudding user response:
This is an example of how to use groupby and sort
data = {'column1' : ['American', 'American', 'Mexican'], 'column2' : [1, 1, 2], 'column3' : [1, 1, 2]}
df = pd.DataFrame(data)
df = df.groupby('column1')[['column2', 'column3']].sum().sort_values(by= "column1", ascending=False)
I added my own dataframe since you are a new contributor and might not know how to create examples
In this example you can see I create a df with some data then group by the 'column1' and get the sum of 'column2' and 'column3' once I have the sum I use the sort_values function to sort by 'column1'
You should be able to plug in your own data and get the desired results.
CodePudding user response:
enter image description hereI apply your dataframe
Aide_a = aide1
za = pd.DataFrame(Aide_a)
za = za.groupby('Pays')[[ 'Aide_kg']].sum().sort_values(by= "Pays", ascending=False).head(30)
za
I woul like order by ascending = false