Pandas python + 值格式 [英] Pandas python + format for values

查看:64
本文介绍了Pandas python + 值格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

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

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

datos1 = (df[(df['Participation'].str.contains('Coordinator')) & (df.Country.str.count('Spain'))])
datos2 = 'Las participaciones en proyectos como coordinador son='
datos4= 'El presupuesto en Euros es ='
display(datos1)
print(datos2, datos1.Country.str.count('Spain').sum())
print(datos4, datos1.Budget.sum())

Year    Project                           Entity                  Participation Country Budget
2015    671598 - 5G-CROSSHAUL   UNIVERSIDAD CARLOS III DE MADRID*   Coordinator Spain   899471.88
2015    671517 - SONATA - 5G    ATOS SPAIN SA*                      Coordinator Spain   602437.50
2015    671704 - CHARISMA - 5G  FUNDACIO PRIVADA I2CAT *FI2CAT      Coordinator Spain   557312.50

我希望以欧元为单位获得相同的结果,我尝试使用以下代码:

I want to have the same result with the budget in euros, i tried with this code:

display(datos4, datos1.Budget.sum().style.format('€ {0:,.0f}'))

更新:

display(datos4, datos1.Budget.sum().style.format('€ {0:,.0f}'))

这一行是用这个代码解决的:

This line was solve with this code:

print(datos4, '€ {0:,.0f}'.format(datos1.Budget.sum()))

现在,我只有预算中的样式格式有问题,我尝试过使用此代码修复:

Now, i only have problems with the style format in the budget, i tried like to fix with this code:

datos1 = (df[(df['Participation'].str.contains('Coordinator')) 
             & (df.Country.str.contains('Greece')) 
             & (df.Budget.apply('€ {0:,.0f}'.format))])

df['Budget']=df.Budget.apply('€ {0:,.0f}'.format)
datos1 = (df[(df['Participation'].str.contains('Coordinator')) 
                 & (df.Country.str.contains('Greece'))]) 

我有代码错误,请问有什么想法吗?

i have errors with the code, please any idea?

推荐答案

我建议您不要将逻辑与外观混为一谈.根据逻辑创建表格,然后对其进行格式化.试试这个代码:

I advise you not to mix logic with appearance. Create the table according to logic, and then format it. Try this code:

datos1= df.loc[ \
                   df.Participation.  \
                   str.contains('Coordinator',case=False) \
                  & df.Country. \
                    str.contains('Greece',case=False) ]

datos1_pretty= datos1.style.format({"Budget":'€ {0:,.0f}'})

您可以在字典中指定更多列.

You can specify more columns in the dict.

这篇关于Pandas python + 值格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆