Pandas python + format for values

2024/9/20 23:21:20

This is the code:

import pandas as     pd
from   pandas import Series, DataFrame
import numpy  as     np
import matplotlib.pyplot as pltdf.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,500datos1 = (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}'))

Update:

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))])

or

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?

Answer

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.

https://en.xdnf.cn/q/119269.html

Related Q&A

Is there any implementation of deconvolution?

Some one may prefer to call it the transposed convolution, as introduced here. Im looking forward to an implementation of the transposed convolution, in Python or C/C++. Thank you all for helping me!

discord.py How to check if user is on server?

I need to check if the user is on the server. Please help me

Inserting variable stored data into SQLite3 - Python 3

I have been reading information on how to insert data into a database using data stored in a variable. I have not been able to get my data to load to my database and I am not sure why.The program is w…

Print specific line in a .txt file in Python?

I have got a .txt file that contains a lot of lines. I would like my program to ask me what line I would like to print and then print it into the python shell. The .txt file is called packages.txt.

EOF error with both exec_command and invoke_shell method in paramiko

I am trying to execute a command on linux server from my windows machine using python paramiko , I used both of the methods1.exec_command2.invoke_shellBoth of them giving EOF error.import paramiko impo…

Trying to simulate an Overwatch loot box opening program in Python

So basically I am trying to recreate opening a loot box in Overwatch into a runnable program in python. Im trying to make it take four random items in an array and display them each time the user types…

How to remove extra commas from data in Python

I have a CSV file through which I am trying to load data into my SQL table containing 2 columns. I have 2 columns and the data is separated by commas, which identify the next field. The second column c…

How to linearize the sum of a product of two decision variables in LP?

Being new to Linear Programming and Gurobi, I am dealing with an Integer Linear program where I have two binary decision variables defined as B[u_v, x_y] and A[u_x], I am trying to implement this const…

Basic calculator program in python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why am I receiving ERROR 404 - when attempting to use Python Flask

I have been following this tutorial: https://kb.objectrocket.com/postgresql/scrape-a-website-to-postgres-with-python-938 My app.py file looks like this (taken from the above tutorial): from flask impor…