Using astype() for Type Conversion in pandas
📖 Scenario: You work in a small store and have a list of products with their prices and quantities. The data was entered as text, but you need to convert the prices and quantities to numbers to do calculations.
🎯 Goal: You will create a pandas DataFrame with product data, then convert the price and quantity columns from text to the correct number types using astype(). Finally, you will print the updated DataFrame.
📋 What You'll Learn
Create a pandas DataFrame with product names, prices, and quantities as strings
Create a variable to hold the list of columns to convert
Use
astype() to convert the price column to float and quantity column to integerPrint the final DataFrame to see the changes
💡 Why This Matters
🌍 Real World
Stores and businesses often get data as text but need to convert it to numbers for calculations like totals and averages.
💼 Career
Data analysts and scientists frequently convert data types to prepare data for analysis and visualization.
Progress0 / 4 steps