About Lesson
Join the conversation

# Function to convert sizes to MB
def convert_to_mb(size):
if "k" in size:
value = float(size.replace("k", ""))
return value / 1024 # Convert KB to MB
elif "M" in size:
return float(size.replace("M", "")) # Already in MB
else:
return None # Handle unexpected values# Apply the conversion function to the column
df["Size_in_MB"] = df["Size"].apply(convert_to_mb)
df.head()
Reply

all assignments sir,
# Function to convert sizes to MB
def convert_to_mb(size):
if 'm' in size.lower(): # Check if the value is in MB
return float(size.lower().replace('m', ''))
elif 'k' in size.lower(): # Check if the value is in KB
return float(size.lower().replace('k', '')) / 1024
else:
return None # Handle unexpected cases# Apply the conversion function to the Size column
df['Size'] = df['Size'].apply(convert_to_mb)
df.head()df["Price"] = df["Price"].str.replace("$","").astype(float)
df['Installs'] = (
df['Installs']
.str.replace(",", "", regex=True)
.str.strip()
.astype(int)
)
Reply

ASSIGNMENT
# Function to convert sizes to MB
def convert_to_mb(size):
if 'm' in size.lower(): # Check if the value is in MB
return float(size.lower().replace('m', ''))
elif 'k' in size.lower(): # Check if the value is in KB
return float(size.lower().replace('k', '')) / 1024
else:
return None # Handle unexpected cases# Apply the conversion function to the Size column
df['Size'] = df['Size'].apply(convert_to_mb)
df.head()
Reply

df['Price'] = df['Price'].str.replace('$', '')# Remove non-numeric values from the Price column
df['Price'] = pd.to_numeric(df['Price'], errors='coerce')# Replace NaN values with 0
df['Price'] = df['Price'].fillna(0)
Reply

df['Installs'] = df['Installs'].str.replace('+', '')# Replace 'Free' values with 0
df['Installs'] = df['Installs'].replace('Free', '0')# Convert the Installs column to integer values
df['Installs'] = df['Installs'].str.replace(',', '').astype(int)# Define the bins for the Installs column
bins_installs = [0, 1000, 10000, 100000, 1000000]# Define the labels for each bin
labels_installs = ['Few', 'Some', 'Many', 'Very Many']# Apply the binning method to the Installs column
df['Installs_Binned'] = pd.cut(df['Installs'], bins=bins_installs, labels=labels_installs)
Reply

def convert_to_mb(size):
if isinstance(size, str):
if size.endswith('k'):
return str(round(float(size[:-1]) / 1024, 2)) + 'M'
elif size.endswith('M'):
return size
elif isinstance(size, float) or isinstance(size, int):
return str(size) + 'M'
else:
return sizedf['Size'] = df['Size'].apply(convert_to_mb)
Reply

# Replace 'Varies with device' with NaN for numerical operations
df['Size'] = df['Size'].replace('Varies with device', np.nan)# Convert Size to numeric, handling 'k' and 'M'
def convert_size(size_str):
if pd.isna(size_str):
return np.nan
elif 'k' in size_str:
return float(size_str.replace('k', '')) / 1024
elif 'M' in size_str:
return float(size_str.replace('M', ''))
else:
return np.nandf['Size'] = df['Size'].apply(convert_size)# Clean 'Installs' column
df['Installs'] = df['Installs'].astype(str).str.replace('+', '').str.replace(',', '')
df['Installs'] = pd.to_numeric(df['Installs'], errors='coerce')#Handle 'Rating' column
df['Rating'] = pd.to_numeric(df['Rating'], errors='coerce')
Reply

I have read both blogs. Both blogs are very informative.
Reply

import numpy as np
kbs = 1024 # example value in KB
mbs = np.round(kbs / 1024, 2)
print(mbs
Reply

assignment done.
Reply