As title, I am new in Python3
I wanna code for reading excel, and formating all DataFrame as Text with leading zero, but if cell Empty then keep it empty
import pandas as pd
import numpy as np
# read data from Excel
df = pd.read_excel(r'C:\... excel.xlsx', sheet_name='per', header=0, usecols='A:N', nrows=101)
#make dataframe
df = pd.DataFrame(df)
# replace empty cells with NaN
df = df.replace("NaN", "")
# Convert all values to string and replace "nan" with empty string
df = df.astype(str).replace("NaN", "")
# Convert all values to string and replace "nan" with empty string
df = df.astype(str).replace("nan", "")
# convert to string with leading zeros
df = df.apply(lambda x: x.astype(str).str.zfill(2))
print(df)
but it returns me some thing like this
number x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13
0 00 03 34.0 44.0 84.0 97.0 00 00 00 00 00 00 00 00
1 01 74 91.0 93.0 99.0 00 00 00 00 00 00 00 00 00
2 02 51 59.0 87.0 00 00 00 00 00 00 00 00 00 00
and I expect like this (text with empty cell):
number x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13
00 03 34 44 84 97
01 74 91 93 99
02 51 59 87
please help.
Thanks
here’s my file
https://www.mediafire.com/file/4f65bk0tx1prh71/example.xlsx/file