Home > Blockchain >  Pandas removes first character from value
Pandas removes first character from value

Time:01-20

I have a dataset where I want to print the specific telephone number belonging to a specific userID. The data set in question is:

#Leads.csv
USR_ID,Name,LastName,Gender,E-mail,Telephone
5iBR72rd28sdKlxSOQMffg,Iubhefsugus,fueshfs,Female,[email protected],0613829902

To do this I run these lines

df = pd.read_csv('Leads.csv')
readrow = df[(df['USR_ID'] == '5iBR72rd28sdKlxSOQMffg')]
print(readrow['Telephone'].item())`

and what I get is: 613829902

Does anyone know why this keeps happening, cause quite frankly I'm stumped. Thanks in advance!

CodePudding user response:

pandas attempts to convert the values to numeric if it can, and leading zeros are stripped out as they are noneffective. You can explicitly say you want "Telephone" column as strings:

>>> pd.read_csv("file.csv", dtype={"Telephone": "str"})

                   USR_ID         Name LastName  Gender      E-mail   Telephone
0  5iBR72rd28sdKlxSOQMffg  Iubhefsugus  fueshfs  Female  [email protected]  0613829902

and we see that leading 0 is there.

CodePudding user response:

If you want to read all columns as string then:

pd.read_csv('Leads.csv', dtype=str)

leading zeros will not be stripped.

  • Related