Whilst exploring a CSV file, I came across some columns which were not according to my needs and I want to change such columns and their values during the import process in Python. I was wondering if there are ways by which I can modify the values or apply some function on those columns before loading the data into a Pandas dataframe
, this way I can save time and reduce the amount of data manipulation needed later on. Please provide such techniques below along with an example code.
Hi @mubashir_rizvi This can be done by using the “dtype” argument of the “read_csv()” function. The dtype
argument of this function allows you to specify the data type of each column in the CSV file as it is being imported into a Pandas data frame.
In the example above, I have changed the datatypes of columns crim
and zn
to string datatypes, which are also known as object
datatypes.