Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to solve issue with groupby on a dataframe

I wish to have a final dataframe with a index of projects, a list of exchanges and a last column for price.

Here is an example:

data = {'Exchange': ['coinbase', 'binance', 'coinbase', 'ftx','coinbase'], 'Projects': ['Bitcoin', 'Bitcoin', 'Ethereum', 'Ethereum','Doge'],'Price': [10,5,10,2,10]}

df = pd.DataFrame(data)

Output : 
   Exchange  Projects  Price
0  coinbase   Bitcoin     10
1   binance   Bitcoin      5
2  coinbase  Ethereum     10
3       ftx  Ethereum      2
4  coinbase      Doge     10

Here is what I tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df2 = df.groupby(by=["Projects"]).count()


df2['Price'] = df['Price']

df2['Exchange'] = df['Exchange']

df2

Output:

          Exchange  Price
Projects        
Bitcoin     NaN      NaN
Doge        NaN      NaN
Ethereum    NaN      NaN

What I wish to have:

            Exchange          Price
Projects        
Bitcoin     coinbase,binance  10
Doge        coinbase,ftx      2
Ethereum    ftx               5

>Solution :

In your case

out = df.groupby('Projects').agg({'Exchange': ','.join,'Price':'last'})
Out[35]: 
                  Exchange  Price
Projects                         
Bitcoin   coinbase,binance      5
Doge              coinbase     10
Ethereum      coinbase,ftx      2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading