[英]Add leading zeros to a column
我有一个包含电话号码的数据列
在读取文件时,pandas 会自动从电话号码中删除前导零。
我无法改变这种行为,但是我尝试添加前导零。
所以我试过这个:
# add the missing leading zeros to phone numbers.
read_file['Phone 1 - Value'] = "0" + read_file['Phone 1 - Value'].astype(str)
print(read_file.head())
但它也不起作用,奇怪的是,当打印 head() 时它确实有效,但是稍后将文件另存为 CSV 文件时,它默认返回到奇怪的行为,如下所示:
read_file.to_csv(export_file_path, index = None, header=True, index_label = True, encoding='utf-8')
有小费吗?
我认为这是熊猫如何读取数字的问题。
在文件中是前导零,但熊猫将值转换为整数,因此已删除。
为了避免将列于字符串由dtype
参数read_csv
:
df = pd.read_csv(export_file_path, dtype={'Phone 1 - Value':str})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.