Olijoh Clarke
Olijoh Clarke

Reputation: 1

Formatting output of floats in a dictionary

Im currently running some correlation in test in python. I get the results I need but they're in x.01234567890 and I'm trying to format them to output them into percentage.

this is reading from a csv.

results = (
    {
        clean_df['NZDUSD_Close'].corr(clean_df['USDJPY_Close']),
        clean_df['NZDUSD_Close'].corr(clean_df['EURGBP_Close']), 
....
        eclean_df['AUDUSD_Close'].corr(clean_df['EURUSD_Close']),
        clean_df['GBPUSD_Close'].corr(clean_df['EURUSD_Close'])
    }
)

print(results)

The above works. There are 15 results and returns all in floats, but I get string errors when I formatted this as a tuple I decided to make it a dictionary.

I've tried the following for formatting the output:

print (f"{results:.2%}")

This works for a single variable but not the list.

for key in results.keys():
   print(key, "{:.2f}".format(results[key]))

Upvotes: 0

Views: 286

Answers (1)

waBoroAsuP
waBoroAsuP

Reputation: 71

If the error your encountering is:

"AttributeError: 'set' object has no attribute 'keys'"

Then it's because your dictionary is inside a tuple and a tuple has no "keys" attribute. You would be best off if you used a tuple:

results = (...)

and printing it like:

print(f"{results:.2%}")

Doesn't work because python doesn't know to iterate over all the objects in the result.

Another way of printing the tuple is:

for val in results:
    print("{:.2%}".format(val))

This will iterate over every value in results and print it in a 2 point percentage format

Upvotes: 1

Related Questions