Pandas read_csv all columns as string

In pandas, the read_csv function allows you to read a CSV file into a DataFrame. By default, it will infer the data types of each column based on the data in the file. However, if you want to read all columns as strings, you can specify the dtype parameter in the read_csv function call.

Here’s an example on how to read a CSV file with all columns as strings:

    
import pandas as pd

# Assuming you have a CSV file named "data.csv" with columns: A, B, C
df = pd.read_csv('data.csv', dtype=str)

# The dataframe will have all columns as string data type
print(df.dtypes)
    
  

The above code snippet imports the pandas library, reads a CSV file named “data.csv” into a DataFrame called df, and specifies the data type for each column as a string using the dtype=str parameter. Finally, it prints the data types of all columns in the DataFrame.

By specifying dtype=str, all columns in the resulting DataFrame will be of type string, regardless of the actual data types in the CSV file. This can be useful when you want to treat all columns as strings, especially if you have mixed data types in your CSV file.

Leave a comment