简体   繁体   English

在读取 pandas 中的文件时自动转换为 integer

[英]Automatic converting to integer while reading a file in pandas

I have a raw data set as.txt file as follows我有一个原始数据集 as.txt 文件如下

ID    SI_Number   
1     0010
2     0005

I am pushing this data to postgresql using psycopg2.我正在使用 psycopg2 将此数据推送到 postgresql。 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.我试过df['SI_Number'].astype(str)但没有结果。

I am reading the.txt file with the following我正在阅读带有以下内容的 .txt 文件

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.我正在使用 Linux 操作系统。 Also while uploading I am converting everything to VARCHAR().此外,在上传时,我将所有内容都转换为 VARCHAR()。

Am I missing out anything?我错过了什么吗?

You can convert to int -> string and then apply a zfill() (zero fill)您可以转换为 int -> string 然后应用zfill() (零填充)

>>> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM