Contents:

Free Python Course ➞ An intensive program for beginners and professionals. Four exciting projects for your portfolio and the opportunity to ask questions to an expert. Learn about the course content!
Learn MoreData Analysis Using Excel
As an example, let's take a table from one of our webinars. It includes a thousand rows of educational information, which collects data on tourists and their travels:

- monthly earnings (salary);
- hometown (city);
- age (age);
- wishes regarding the type of vacation.
- Preferred mode of travel.
- number of members Families;
- And, ultimately, the city the traveler decided to visit.
We will be using the free Google Colab service, which allows you to write and run code to process data directly in your browser, without requiring the installation of any additional applications. Check out our article to learn how to get started.
Transforming Information: Converting Words into Numeric Values
The salary column is a convenient element for machine learning, since it contains exclusively numeric values. However, our table also contains other columns that may cause difficulties for algorithms, such as city names, vacation types, transportation preferences, and the target city. These data are categorical features.
We need to transform these text features into numeric values. To do this, each feature value will be transferred to a separate column, which will then be added to the source table.
The city column represents 11 cities. This means that 11 new columns will be added to the original table, each named after a corresponding city. For example, if a tourist is from Yaroslavl, the city_Yaroslavl column will contain a one, while the other ten columns, representing other cities, will contain zeros.
The process of converting words into numeric values is known as categorical variable encoding. The method we're going to use is called one-hot encoding. There are other, more sophisticated techniques for transforming non-numeric columns.
We'll handle the vacation preference and transport preference columns similarly: if, for example, one of the tourists prefers the train, then the newly added transport_preference_Train column will contain one, and the other columns will contain zeros.
The Pandas library provides a get_dummies() function for performing one-touch encoding.
What happened here? We created a new variable, trips_df_2, using the Pandas library's .get_dummies() method. In doing so, we carried over the data from our previous trips_df variable, transforming the values in the city, vacation_preference, and transport_preference columns. into separate columns.
As a result, we have created a table containing 24 columns. Now we can get a list of just the names of these columns using the .columns attribute:

It is observed that the previous columns, such as income, age, and family size, remain unchanged, while instead of categorical variables, a lot of new numerical data is now presented.

