1

I am using JetBrains Pycharm (Windows OS) for python coding. In my code, I read a csv file using pd.read_csv(dataset). Then do few moderation on some of the table columns & finally want to see the updated table, so at the end give the command --> print (dataset).

Now when the modified dataset is printed at the bottom window, all the elements of the dataset are printed in floating point exponential format.

e.g., [ 0.00000000e+00 0.00000000e+00 1.00000000e+00 1.65349200e+05 1.36897800e+05 4.71784100e+05]

This is where I need the help. I need the dataset to be printed in flaoting point decimal format or simple decimal format.

How that is possible in the easiest way ? Also, is there any option in Pycharm, to configure the output format (exponential, decimal etc..) from any setting/configuration window, instead of hard-coding ?

1

1 Answer 1

1

Use {n}. format ()

For ex: '{:n}'.format(1234))

ds_x=[ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00,1.65349200e+05, 1.36897800e+05, 4.71784100e+05]
# For Numpy arrays use '.astype'
# Copy of the array 'ds_x', cast to a specified type 'float'.
# Note: Try type 'float32' OR 'float64' if the below fails to provide correct precision.
data_set = ds_x.astype(float)
for i in data_set:
    print("{:.16f}".format(float(i)))

# More about better precision can be found here

Output:

0.0000000000000000

0.0000000000000000

1.0000000000000000

165349.2000000000116415

136897.7999999999883585

471784.0999999999767169

Or you can use the alternative way:

"%.16f" % (float( 00,1.65349200e+05))

The second question regarding PyCharm. You may want to look into formatting markers . Complete details on reformatting .


Update:

Here is a updated code sample with the dataset that was provided by you.

import numpy as np

ds = np.array([[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00, 2.86637600e+04, 1.27056210e+05, 2.01126820e+05],
                [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00, 1.44372410e+05, 1.18671850e+05, 3.83199620e+05]])

for data_x in iter(ds):
    print()
    for data_y in data_x:
        print("%.16f" % float(data_y))

output:

0.0000000000000000
1.0000000000000000
0.0000000000000000
28663.7599999999983993
127056.2100000000064028
201126.8200000000069849

0.0000000000000000
0.0000000000000000
1.0000000000000000
144372.4100000000034925
118671.8500000000058208
383199.6199999999953434

Regarding the error:

TypeError: only size-1 arrays can be converted to Python scalars

This is because the dataset that you are using is a Python matrix OR a list of a list / two arrays. The first code sample in this post is using one loop which works with one array; however, since you are using a larger dataset containing two arrays on the first loop it will pass the entire array causing the above error to be outputted. To correct this I simply added a second loop to iterate through each value for each array.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. In my case I want to apply it on a big dataset (ds) having multiple rows & columns. So what I type is - for i in ds_x: print(print("{:.16f}".format(float(i)))) I got the error - line 20, in <module> print("%.2f" % (float(i))) TypeError: only length-1 arrays can be converted to Python scalars
The appears to be numpy related stackoverflow.com/a/36680545/7548672 - try doing data_set = ds_x.astype(float) before the for loop in my above example.
Thanks. I'm unlucky again. It still throws the same error - TypeError: only length-1 arrays can be converted to Python scalars. If I print my dataset (ds), it shows the below, which will give you a better idea, what exactly my dataset is - print (ds) [[ 0.00000000e+00 1.00000000e+00 0.00000000e+00 2.86637600e+04 1.27056210e+05 2.01126820e+05] [ 0.00000000e+00 0.00000000e+00 1.00000000e+00 1.44372410e+05 1.18671850e+05 3.83199620e+05]]
I added an update to my post using your dataset. If it is still not what you are looking for, could you please possibly update your original post providing your code which is not working. I will try to edit what I see could be the issue. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.