Python Forum

Full Version: How to keep leading zeros with pandas?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a csv file with 4 columns.
col 1 (numbers)
col 2 (text)
col 3 (numbers)
col 4 (text)

technically all four columns have a format type of general, basically my number are not really numbers which is fine.

When I import into pandas it removes my leading zeros for ONLY 3 (col 1 loads fine for some reason). How can I keep the leading zeros for col 3? I guess I have to tell pandas to treat those columns as strings?
df = pd.read_csv("myfile.csv")
*I am new to python
You can specify the column types instead of letting the read_csv file guess.

dtype
Type name or dict of column -> type, optional
Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’} Use str or object together with suitable na values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion.