About Lesson
Join the conversation

df['Size'] = df['Size'].replace('Varies with device', np.nan)
# Convert KBs to MB
df['Size'] = df['Size'].apply(lambda x: float(x.replace('k', ''))/1024 if 'k' in str(x)
else float(x.replace('M', '')) if 'M' in str(x)
else float(x))
Reply

# THIS IS FOR PRICE COLUMN
df['Price'] = df['Price'].astype(str).replace("$","",regex=False).astype(float)
Reply

def convert_installs(value):
return int(value.replace("+","").replace(",",""))df['Installs'] = df['Installs'].astype(str).map(convert_installs)#THIS IS FOR INSTALL COLUMNS
Reply

# THIS IS FOR SIZE SECTION
def Convert_Size(value):
if 'M' in value:
return float(value.replace("M",""))* 1_000_000
elif 'k' in value:
return float(value.replace("k",""))* 1_000
elif value == 'Varies with device':
return None
else:
return float(value)df['Size'] = df['Size'].astype(str).map(Convert_Size)
Reply

# 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