1

When trying to save some number as a string type in CSV file, then saving this file and reading it again, the file shows this saved data as a numpy.int64 instead of a string. How can this be solved so when reading the csv file it reads it as a string, not int?

Here is a Python script that describes this case

import pandas as pd

df = pd.DataFrame(columns=['ID'])

ID = '1'
df = df.append(pd.DataFrame([[ID]], columns=['ID']))
df.to_csv('test.csv', index=False)

"""
now the csv file looks like this:

ID
1
"""

df = pd.read_csv('test.csv')
print(df['ID'].iloc[0] == ID)  # this will print False
print(type(df['ID'].iloc[0]))  # this will print <class 'numpy.int64'>
1
  • 2
    A csv file is text. The entries don't have a data type (other than just text, I suppose). Commented Sep 29, 2021 at 22:38

1 Answer 1

2

The CSV file format doesn't distinguish between different data types. You have to specify the data type when reading the CSV with pandas.

df = pd.read_csv("test.csv", dtype=str)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.