Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Replacing Characters | Preprocessing Data: Part I
Analyzing and Visualizing Real-World Data

book
Replacing Characters

Just as we removed characters, there are also two ways to perform replacements: either by using the .str accessor or by using a lambda function. Since we are only performing the replacement for one column, let's use the first approach.

Tehtävä

Swipe to start coding

  1. Replace the commas with dots in the 'CPI' column. Follow these steps:

    • Select the 'CPI' column;
    • Use the .str accessor;
    • Replace the ',' symbols with '.' using the .replace() method;
    • Convert the values to float type;
    • Assign the obtained result to the 'CPI' column.
  2. Display the first row of the df dataframe and data types of the df dataframe.

Ratkaisu

# Load the library
import pandas as pd

# Read the data
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/72be5dde-f3e6-4c40-8881-e1d97ae31287/shops_data1.csv')

# Convert the 'CPI' column into numerical
df['CPI'] = df['CPI'].str.replace(',', '.').astype(float)

# Display the first row of dataframe and dtypes
print(df.head(1))
print(df.dtypes)

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 7
# Load the library
import pandas as pd

# Read the data
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/72be5dde-f3e6-4c40-8881-e1d97ae31287/shops_data1.csv')

# Convert the 'CPI' column into numerical
df['___'] = df['___'].___.___('___', '___').___(___)

# Display the first row of dataframe and dtypes
print(df.head(___))
print(df.___)

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt