The numerical data read from Excel is output in a different form.

Asked 2 months ago, Updated 2 months ago, 2 views

When numerical data read from Excel is displayed on vscode, it is displayed up to a decimal point.
For example, a value of 2 indicates 2.0.I'd like to leave out the decimal point.I'd like to print 0001 as 1.0 while it's still 0001.

·I want to output Excel data values as they are
·The output [.0] on the vscode should not be displayed.
If both are problems with Excel configuration, I would appreciate it if you could let me know.

import padas as pd
df=pd.read_excel("check.xlsx", sheet_name="Sheet1")
for df_chindf ["key"]:
    if str((df_ch)) .isdigit():
        print ("Number")
    else:
        print("Not a number")

Above: Excel data
Bottom: vscode output screen
Exceldata

Enter a description of the image here

python pandas

2022-09-30 11:11

2 Answers

You can specify .0f as the formatting in the Pandas option.

pd.options.display.float_format='{:0f}'.format

reference:
Valid digits (valid digits):display.float_format|pandas display settings change


2022-09-30 11:11

0001 is displayed as 1.0 because it is automatically determined to be of type float during capture.
If you want to keep 0001 and output it without dropping the previous zero, specify dtype=str during import and import it as a string.

The above mentioned content is mentioned in multiple answers to Previous question.
There are more detailed explanations than this answer, so we recommend that you read them again.

sample code

import pandas as pd

df=pd.read_excel("test.xlsx", dtype=str)
print(df)


2022-09-30 11:11

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.