I have an Excel file. One of the columns is type General. Values in this column have formats like 10cm, 0, 1, TRUE and FALSE. Now I'm using Pandas to parse the Excel file into a dataframe and then save them in a table in SQLite. I want the values in this column keep its own format.
I have used xl=pd.ExcelFile() to read the file and xl.parse() to read the data. But the FALSE value in the column become 0, which confuses me with real 0 value. The code line is
df=xl.parse(sheet_name=sheet,names=columns,skiprows=2)
where the sheet is a given sheetname and columns is a list of column names.
Could someone help me with the issue?
CodePudding user response:
This is how I got it-
df = pd.read_excel('C:\\Users\\Downloads\\Practice\\Book1.xlsx',header=0,converters={'General':str})
df
Output
General 10cm 0 1 True False
CodePudding user response:
You can specify it's type using the dtype parameter. Please try with:
df = pd.read_excel('file_path',sheet_name=sheet,skiprows=2,dtype={'General':object})
CodePudding user response:
Try mapping TRUE and FALSE to string object before you parse to excel like this
pd['General'] = pd['General'].map({True: 'True', False: 'False'})
Or you can use replace with the same impact
pd['General'] = pd['General'].replace({True: 'True', False: 'False'})

