Removing Characters: Method 2
As mentioned, there are two ways to remove a character from all column values. The second method uses a lambda
function. How does it work? You define a lambda
function that removes a certain character/characters from a function variable, and apply it to the selected column. Then you convert obtained values to the necessary type and save them.
Tarea
Swipe to start coding
- Define a lambda function with a single argument
x
that will look for and delete any of the characters'$°C%'
from both the left and right sides (using the.strip()
method). Assign the function to therm
variable. - Apply the
rm
function to the'Fuel_Price'
column and then convert it to numerical type (float
) using the.astype()
method. Assign the obtained result to the same column. - Perform the same actions described in the step 2 for the
'Unemployment'
column. - Perform the same actions described in the step 2 for the
'Temperature'
column. - Display the first row of the
df
dataframe and data types of thedf
dataframe.
Solución
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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_data_init.csv')
# Define a lambda function
rm = lambda x: x.strip('$°C%')
# Convert the 'Fuel_Price' column into numerical
df['Fuel_Price'] = df['Fuel_Price'].apply(rm).astype(float)
# Convert the 'Unemployment' column into numerical
df['Unemployment'] = df['Unemployment'].apply(rm).astype(float)
# Convert the 'Temperature' column into numerical
df['Temperature'] = df['Temperature'].apply(rm).astype(float)
# Display the first row of dataframe and dtypes
print(df.head(1))
print(df.dtypes)
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 1. Capítulo 5
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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_data_init.csv')
# Define a lambda function
rm = ___ ___: x.___('___')
# Convert the 'Fuel_Price' column into numerical
df['Fuel_Price'] = df['___'].apply(___).___(float)
# Convert the 'Unemployment' column into numerical
df['Unemployment'] = df['Unemployment'].___(___).astype(___)
# Convert the 'Temperature' column into numerical
df['___'] = df['___'].___(rm).astype(___)
# Display the first row of dataframe and dtypes
print(df.___(___))
print(df.___)
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla