Search code examples
pythonpandasgroup-bypandas-groupbysklearn-pandas

How to filter data with Style format in Euros + pandas Python


I have a database and i have some problems when i used the Style format(Euros)

df.head(3).style.format({'Budget': "€{:,.0f}"})
    Year    Project Entity  Participation   Country Budget
0   2015    671650 - MMMAGIC - 5G   'FUNDACION IMDEA NETWORK*'  Participant Spain   €384,000
1   2015    671650 - MMMAGIC - 5G   'ROHDE & SCHWARZ GMBH*' Participant Germany €12,000
2   2015    671650 - MMMAGIC - 5G   'SAMSUNG ELECTRONICS (UK) LIMITED'  Coordinator UnitedKingdom   €997,500

The problem is when i apply this filter:

display(df.groupby('Entity')['Budget'].agg(['sum'])
.sort_values('sum', ascending=False).head(3).style.format('€ {0:,.0f}'))

                                                sum

Entity
FRAUNHOFER GESELLSCHAFT EV € 18,550,842 TELEFONICA INVESTIGACION Y DESARROLLO SA* € 13,592,263 ORANGE SA* € 9,517,402

I will like to have a result like this:

Country        Entity                                                                             
Germany  FRAUNHOFER GESELLSCHAFT EV                     € 18550842.50
Spain    TELEFONICA INVESTIGACION Y DESARROLLO SA*      € 13592263.26
France   ORANGE SA*                                     € 9517402.06

but when i applied the command:

df.groupby(['Country', 'Entity'])['Budget'].sum().sort_values(ascending=False)

i obtained the same but without euros symbol, please any suggestion to resolve this issue.


Solution

  • You got a Series object that has no 'style' method, so you must call 'to_frame()':

    df.groupby(['Country', 'Entity'])['Budget'].sum()  \
                 .sort_values(ascending=False)  \
                 .to_frame()  \
                 .style.format('€ {0:,.0f}')