如何对一些结果进行分组和汇总(欧元样式格式)

2024-10-02 08:25:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个饼图,在欧洲和一些特定的国家,我需要分组和总和一些国家或公司在一个小组称为“其他”,例如:所有公司的预算低于10000欧元。你知道吗

import pandas as     pd
from   pandas import Series, DataFrame
import numpy  as     np
import matplotlib.pyplot as plt

    Year    Project Entity  Participation   Country Budget
0   2015    671650 - MMMAGIC - 5G   FUNDACION IMDEA NETWORK*    Participant Spain   € 304,000
1   2015    671650 - MMMAGIC - 5G   ROHDE & SCHWARZ GMBH*   Participant Germany € 120,000
2   2015    671650 - MMMAGIC - 5G   SAMSUNG ELECTRONICS (UK) LIMITED    Coordinator UnitedKingdom   € 797,500
3   2015    671650 - MMMAGIC - 5G   HUAWEI TECHNOLOGIES DUESSELDORF GMBH    Participant Germany € 648,000
4   2015    671650 - MMMAGIC - 5G   TELEFONICA INVESTIGACION Y DESARROLLO SA*   Participant Spain   € 531,976
5   2015    671650 - MMMAGIC - 5G   CHALMERS TEKNISKA HOGSKOLA AB*  Participant Sweden  € 233,000

datos1 = (df[(df['Project'].str.contains('5G-MOBIX')) & (df.Country.str.count('Spain'))])
datos2 = 'La cantidad de proyectos es ='
datos4= 'El presupuesto total en Euros es ='
print(df[(df['Project'].str.contains('5G-MOBIX')) & (df.Country.str.count('Spain'))& (df['Country'].replace(!=''NOKIA SPAIN SA'','Other')]) 
print(datos2, datos1.Country.str.count('Spain').sum())
print(datos4, datos1.Budget.sum())

“替换”命令出错。你知道吗

datos1 = (df[(df['Project'].str.contains('5G-MOBIX')) & (df.Country.str.count('Spain'))])
datos2 = 'La cantidad de proyectos es ='
datos4= 'El presupuesto total en Euros es ='
print(df[(df['Project'].str.contains('5G-MOBIX')) & (df.Country.str.count('Spain'))& (df['Country'].replace(!=''NOKIA SPAIN SA'','Other')]) 
print(datos2, datos1.Country.str.count('Spain').sum())
print(datos4, datos1.Budget.sum())

explode = (0.1, 0, 0, 0)
datos1.groupby("Entity")["Budget"].sum().plot(kind="pie", explode=explode, counterclock=False, autopct='%1.1f%%', shadow=True, startangle=90, figsize=(10, 8))
plt.axis('equal')
plt.title("SPAIN in the project 5G-MOBIX", bbox={"facecolor":"0.5", "pad":5}, color='b')
plt.ylabel('')
plt.show()

我预计饼图中会出现这样的结果:

     Year       Project     Entity  Country   Budget                                                                             
742  2018   825496 - 5G-MM  'NOK'   Spain     € 556815.21                                                                       
743  2018   825496 - 5G-MM  'TDE'   Spain     € 747879.88                                                              
778  2018   825496 - 5G-MM  'ALS'   Spain     € 152868.86                                                                        
780  2018   825496 - 5G-MM  'OTHER' Spain     € 300000 

Tags: importprojectdfcountpltcountrybudgetsum

热门问题