Python - 将Zip代码作为字符串加载到DataFrame? [英] Python - Loading Zip Codes into a DataFrame as Strings?
问题描述
我正在使用熊猫加载包含邮政编码的Excel电子表格(例如32771)。邮政编码作为电子表格中的5位数字字符串存储。当使用命令将它们拉入DataFrame ...
xls = pd.ExcelFile(5-Digit-Zip- code.xlsx)
dfz = xls.parse('邮政编码')
他们被转换成数字。所以'00501'变成501.
所以我的问题是,我如何:
a。加载DataFrame并保存存储在Excel文件中的邮政编码的字符串类型?
b。将DataFrame中的数字转换成五位数的字符串,例如501变成00501?
作为解决方法,您可以转换 int
s使用
Series.str.zfill
:
df ['zipcode'] = df ['zipcode']。astype(str).str.zfill(5)
演示:
导入熊猫为pd
df = pd.DataFrame({'zipcode':['00501']})
df.to_excel('/ tmp / out.xlsx')
xl = pd .ExcelFile('/ tmp / out.xlsx')
df = xl.parse('Sheet1')
df ['zipcode'] = df ['zipcode']。astype(str) $ z
$($)
$ / b
邮政编码
0 00501
I'm using Pandas to load an Excel spreadsheet which contains zip code (e.g. 32771). The zip codes are stored as 5 digit strings in spreadsheet. When they are pulled into a DataFrame using the command...
xls = pd.ExcelFile("5-Digit-Zip-Codes.xlsx")
dfz = xls.parse('Zip Codes')
they are converted into numbers. So '00501' becomes 501.
So my questions are, how do I:
a. Load the DataFrame and keep the string type of the zip codes stored in the Excel file?
b. Convert the numbers in the DataFrame into a five digit string e.g. "501" becomes "00501"?
解决方案 As a workaround, you could convert the int
s to 0-padded strings of length 5 using Series.str.zfill
:
df['zipcode'] = df['zipcode'].astype(str).str.zfill(5)
Demo:
import pandas as pd
df = pd.DataFrame({'zipcode':['00501']})
df.to_excel('/tmp/out.xlsx')
xl = pd.ExcelFile('/tmp/out.xlsx')
df = xl.parse('Sheet1')
df['zipcode'] = df['zipcode'].astype(str).str.zfill(5)
print(df)
yields
zipcode
0 00501
这篇关于Python - 将Zip代码作为字符串加载到DataFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!