How to keep leading zeros in a column when reading CSV with Pandas?
csv, pandas, python, types
Solution
As indicated in this answer by Lev Landau, there could be a simple solution to use `converters` option for a certain column in `read_csv` function.
converters={'column_name': str}
Let's say I have csv file `projects.csv` like below:
project_name,project_id
Some Project,000245
Another Project,000478
As for example below code is trimming leading zeros:
from pandas import read_csv
dataframe = read_csv('projects.csv')
print dataframe
Result:
project_name project_id
0 Some Project 245
1 Another Project 478
Solution code example:
from pandas import read_csv
dataframe = read_csv('projects.csv', converters={'project_id': str})
print dataframe
Required result:
project_name project_id
0 Some Project 000245
1 Another Project 000478
To have all columns as str:
pd.read_csv('sample.csv', dtype=str)
To have certain columns as str:
# column names which need to be string
lst_str_cols = ['prefix', 'serial']
dict_dtypes = {x: 'str' for x in lst_str_cols}
pd.read_csv('sample.csv', dtype=dict_dtypes)
Problem
I am importing study data into a Pandas data frame using `read_csv`. My subject codes are 6 numbers coding, among others, the day of birth. For some of my subjects this results in a code with a leading zero (e.g. "010816"). When I import into Pandas, the leading zero is stripped of and the column is formatted as `int64`. Is there a way to import this column unchanged maybe as a string? I tried using a custom converter for the column, but it does not work - it seems as if the custom conversion takes place before Pandas converts to int.