Monday 15 February 2010

python - Pandas DataFrame Float Formatting -


I have a panda dataframe of survey responses, which I am collecting the average score and output to HTML . My current code looks like this:

  Import as import pandas np df = pd.read_csv ('survey_scores.csv', header = 0) np.round (pd.DataFrame ( Df) Overall ('mean') score) .transpose (), 2) .to_html ()  

It takes a dataframe that looks a bit like this in the CDS ...

  response_number, question_number, score1, 1, 3.0 1, 2, 4.0, 3, 4.0 2, 1, 4.0 2, 2, 4.0 2, 3, 1.0  / Pre> 

and it excludes average digits in an HTML table that formats such a score value:

  3.5, 4, 2.5  

ha However, I am trying to get the output to force each digit to display two digits after the decimal point. I found it to be rounded to two decimal digits, but I have difficulty getting my output to format such values:

  3.50, 4.00, 2.50 < / Code> 

How do I format these values ​​in two decimal places?

I have solved the problem. I Pandas.DataFrame.to_html () can format the float.

Import as pd import NP DF = pd.read_csv ('survey_scores.csv', header = 0) as np.round (pd) pandas altogether ('mean'). Score). Management (), 2) .to_html (float_format = lambda x: '% .2f'% x)


No comments:

Post a Comment