1. Data types
You already know that type() returns the type of an object.
2. Type
However, when working with DataFrames, you may need more information, such as the type of each column in the DataFrame.
3. Info Method
To do this, you can make use of the info() method. This will show you the type of each column in the dataframe, along with other information such as the number of rows, column names, number of non-missing values per column, type, etc..
4. Convert to string
Similar to R, you can easily change the data types of columns by making use of the astype() method. To convert the type of column A you can call astype (str) and assign the result back. Inspecting the output of info() shows that the type of A is now object.
5. String objects
The object type in pandas refers to strings. You will encounter a lot of strings when working with data, so knowing how to manipulate strings is important.
Python allows you to use its built-in string manipulation functions with the str accessor.
6. String accessor
strip() is the built-in string method that removes all leading and trailing whitespaces. As you can see here, you call strip() using the str accessor on the column name.
7. Category
Pandas also provides the category type, which is analogous to factors in R. You can convert a column into a categorical data type by passing the string 'category' to the astype() method.
8. Category accessor
Once you have a categorical column, you can see the various categories (also called levels in R) by calling the cat accessor along with the categories attribute on the column. To see the codes, you can call the codes attribute.
9. Datetime
You will encounter dates frequently in Data Science. You can use the to_datetime() function from pandas to convert strings into dates.
10. Datetime accessor
Just like with strings and categorical values, you can access date components with the dt accessor. The day, month and year attributes give you the day, month and year of the date, respectively.
11. Let's practice!
Time to put this into practice.