简体   繁体   English

如何使用熊猫从带有填充 0 的 csv 中打印电话号码?

[英]How do I print out the Phone number from a csv with padded 0 using pandas?

So I have a CSV file with the following content:所以我有一个包含以下内容的 CSV 文件:

Person,Phone
One,08001111111
Two,08002222222
Three,08003333333

When I used the following code:当我使用以下代码时:

import pandas as pd
df = pd.read_csv('test_stuff.csv')
print(df)

It prints out:它打印出来:

  Person       Phone
0    One  8001111111
1    Two  8002222222
2  Three  8003333333

It removed the starting 0 from the Phone column.它从电话列中删除了起始 0。 I then tried to add the phone as string in the csv file, like so:然后我尝试将电话作为字符串添加到 csv 文件中,如下所示:

Person,Phone
One,'08001111111'
Two,'08002222222'
Three,'08003333333'

However, the result is now this:然而,现在的结果是这样的:

  Person          Phone
0    One  '08001111111'
1    Two  '08002222222'
2  Three  '08003333333'

What can I do to resolve this?我能做些什么来解决这个问题? I am hoping for a result like this:我希望得到这样的结果:

  Person        Phone
0    One  08001111111
1    Two  08002222222
2  Three  08003333333

Thanks in advance.提前致谢。

Don't try to add the zeros back, just don't delete them in the first place by telling pandas.read_csv that you column is a string:不要尝试将零添加回来,只是不要通过告诉pandas.read_csv你的列是一个字符串来删除它们:

pd.read_csv('test_stuff.csv', dtype={'Phone': 'str'})

output:输出:

  Person        Phone
0    One  08001111111
1    Two  08002222222
2  Three  08003333333

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

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