Automatic converting to integer while reading a file in pandas

pythondumb

I have a raw data set as .txt file as follows

ID    SI_Number   
1     0010
2     0005

I am pushing this data to postgresql using psycopg2. In the DB I am seeing the following

ID     SI_Number
1      10.0
2      5.0

In order to get rid of unwanted decimal places, I am using the following piece of code.

df['SI_number'] = df['SI_number'].astype(str).str.replace(r'.0$','',regex=True)

With this in the DB, I am seeing

ID    SI_Number
1      10
2      5

But I want to populate the exact value in the DB. Like below:

ID    SI_Number
1      0010
2      0005

I have tried df['SI_Number'].astype(str) but no result.

I am reading the .txt file with the following

df = pd.read_csv(f,usecols=col_lst,sep='|',engine='python',encoding='iso-8859-1',error_bad_lines=False, warn_bad_lines=True)

I am using Linux OS. Also while uploading I am converting everything to VARCHAR().

Am I missing out anything?

Paulo Marques

You can convert to int -> string and then apply a zfill() (zero fill)

>>> df = pd.DataFrame({"A": [1.0, 2.0, 30.0]})

>>> df
      A
0   1.0
1   2.0
2  30.0

>>> df["A"] = df["A"].astype(int).astype(str).apply(lambda x: x.zfill(4))

>>> df
      A
0  0001
1  0002
2  0030

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting hex to int while reading a text file

Converting String to an Integer/float when reading from a csv file in Python

Converting char * to double while reading from binary file in C++?

how to distungish a integer variable while reading a file in c program

How to convert string values to integer values while reading a CSV file?

Getting empty file while converting XML to CSV file Pandas Python

reading file to list<integer>

Pandas - Converting Derived Datetime to Integer

Missing first row while reading from file - Python Pandas

Python: Replace values while reading CSV file with pandas

How to drop a specific column of csv file while reading it using pandas?

Python pandas error while reading and writing csv file

RunTimeError while reading tab separated text file into Pandas dataframe

handle datatype issue while reading excel file to pandas dataframe

Reading an xml file and converting it to a string

setTimeout while reading file

NoSuchFileException while reading a file

Why I'm getting Error while converting a column to datetime object while loading a csv file in pandas?

Ambiguities while converting List<Object[]> to List<Integer>

Pagination: Error While Converting Parameter Value into Integer

NumberFormatException while converting a string to an integer value in java

converting integer to decimal values while adding both

Getting NumberFormatException while Converting character String to Integer

converting hex string to an integer while preserving the format

How to use the variable value(which is integer) into python script while reading a csv file for a particular column

How to make the first first row as header when reading a file in PySpark and converting it to Pandas Dataframe

pandas - reading column as float, converting invalid entries

Reading encrypted file with pandas

Reading an excel file in pandas