Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Managing an Incorrect Column | Preprocessing Data
Advanced Techniques in pandas

book
Managing an Incorrect Column

So, you received the result object. This means that the type of the column is non-numerical, but to calculate necessary values, the column need to be numerical. Let's change that.

  1. Firstly, we need to replace - with .. To do so, you will apply the method .str.replace() to replace the character in the string in the dataset column. The syntax is
    data['column_name'].str.replace('old_symbol','new_symbol')
    In our case, old_symbol is -, and . is the new_symbol;
  2. Then, convert the column to the float data type. To do so, use .astype() method. The syntax is data['column_name'].astype('type').
    In our case, the type is 'float'.
Завдання

Swipe to start coding

Your task is to:

  1. Follow the algorithm above and firstly replace - with . in the column 'Fare'.
  2. Convert the column 'Fare' to the 'float' data type.
  3. Output the type of the column 'Fare'.

Рішення

import pandas as pd

data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/titanic3.csv', index_col = 0)

# Replace `-` with the `.`
data['Fare'] = data['Fare'].str.replace('-', '.')
# Convert column to the float type of data
data['Fare'] = data['Fare'].astype('float')

# Output the type of the column 'Fare'
print(data['Fare'].dtypes)
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 8
import pandas as pd

data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/titanic3.csv', index_col = 0)

# Replace `-` with the `.`
data['Fare'] = data___.___.___(___, '___')
# Convert column to the float type of data
data['Fare'] = data['Fare'].___('___')

# Output the type of the column 'Fare'
print(___)
toggle bottom row
some-alt