Want to learn more? Take the full course at https://learn.datacamp.com/courses/ob... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Now that we've reviewed some Pandas basics, we need to start thinking about other steps we have to take in order to prepare data for modeling. One of these steps is to think about the types that are present in your dataset, because you'll likely have to transform some of these columns to other types later on. Let's take a deeper look at types as well as how to convert column types in your dataset.
Recall that you can check the types of a dataframe by using the dtypes attribute, like this.
Pandas datatypes are similar to native python types, but there are a couple of things to be aware of. The most common types you'll be working with are object, int64, and float64 types. The object type is what Pandas uses to refer to a column that consists of string values or is of mixed types. int64 is equivalent to the Python integer type. the 64 simply refers to the allocation of memory alloted for storing the values. and float64 is equivalent to the float type. Another type you might see as you work with data in pandas is the datetime64 type (or the timedelta type). This is because you can store dates as datetime types in pandas dataframes, and even use datetimes as a special kind of index. All you need to be familiar with as we work through this course are the object, int64, and float64 types, though.
Before any preprocessing can begin, you have to understand what types you're dealing with in your dataset. Sometimes, you'll start working with a dataset that has an incorrect column type: maybe a numerical column was written out into a csv as a string, and when you try to work with that column, numerical operations won't work.
Let's take a look at how to adjust the type of a column if the type that pandas has inferred upon reading in the file is incorrect. Here we have a simple dataset with a couple of columns. if you run df.dtypes, you'll see that the type for column C is object. However, if we simply look at this dataframe, you can see that these are float values: numbers with decimal points. If we want to preprocess and model this data, we're going to have to adjust the column type.
Changing the type of a column is very straightforward. Pandas already has a method for converting the type of the column to a new type. You can change the type using the astype method and passing in the type you want to convert it to. Make sure you're only assigning it to the column you want converted.
It's also good to be as sure as you can that the column type you want to convert to is representative of the whole column. Remember that the object type can represent a column that includes both string and numeric types.
Now it's your turn to do some type conversion.
#Python #PythonTutorial #DataCamp #Preprocessing #MachineLearning
On this page of the site you can watch the video online Python Tutorial: Working with data types with a duration of hours minute second in good quality, which was uploaded by the user DataCamp 11 April 2020, share the link with friends and acquaintances, this video has already been watched 83 times on youtube and it was liked by 0 viewers. Enjoy your viewing!