Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge | Preprocessing Data: Part I
Data Manipulation using pandas

book
Challenge

Try to apply the aquired knowledge to fix the first problematic column.

Task

Swipe to start coding

You are given the Statvillage dataset. You need to convert the 'totinch' column' values to float type by replacing commas by dots. Follow the next steps:

  1. Select the 'totinch' column.
  2. Apply the str accessor to the chosen column.
  3. Replace the , symbols in the column values with the ..
  4. Convert type to float.

Solution

# Importing the library
import pandas as pd

# Reading the data
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data.csv')

# Perform a replacement and convert column type
df['totinch'] = df.totinch.str.replace(',', '.').astype(float)
# Testing
print(df.totinch)

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
# Importing the library
import pandas as pd

# Reading the data
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data.csv')

# Perform a replacement and convert column type
df['totinch'] = ___.___.___.___(___, ___).___(___)
# Testing
print(df.totinch)
toggle bottom row
some-alt