This warning is trying to let you know that some of the columns of your DataFrame failed to resolve to a single data type (at least from pandas' perspective, it didn't know what to interpret the columns as!). Since it's a warning, pandas is happy to give you the DataFrame after finishing the read_csv
but it's not advisable to proceed without addressing your mixed type columns in some fashion.
1) You can avoid this warning by explicitly identifying the dtype
. The parameter in read_csv
is conveniently called dtype
. The usage specified by the doc is either single data type (e.g. dtype = str
) or a dictionary of columns and data types (e.g. dtype = {0:str, 1:int}
). Some options for the data type are:
- the pandas specific data types (
object
)
- the built in Python types (
str
, int
, float
)
- or numpy data types (
numpy.int32
, numpy.float64
)
The nice thing about specifying per column data types is that Python will throw a hard error if something breaks that type convention. It's a great, immediate check that your data is in a format you expect and it's always nice for Python to break loudly. However, it's possible you want to just read all your data in and then fix it in the DataFrame. You generally can get away with specifying dtype=str
and pandas will (usually) perform the read_csv
without complaint.
2) Since this question is unfortunately best answered as "it depends," I would advise looking into data cleaning methodologies (since that's what this problem largely is). Some specific (but generic) advice is: think hard about how you want to deal with your missing/malformed data, be explicit about the transformations you'll apply to the data to handle those cases, and keep track of the fact you've made those changes.
I hope this was helpful!