Data Science & Machine Learning
June 12, 2025 at 06:28 AM
Today, let's move to the next topic in the Data Science Learning Series:
🔹 *Data Type Conversions*
Every column in a DataFrame has a data type like:
int64 → whole numbers
float64 → decimal numbers
object → text or mixed data
bool → True/False
datetime64 → dates & times
*Using the right data type:*
- Saves memory
- Improves performance
- Helps with accurate analysis & modeling
🔍 *How to Check Data Types*
df.dtypes
🔁 *Convert Data Types in Pandas*
✅ 1. String to Integer
df['column'] = df['column'].astype(int)
✅ 2. String to Float
df['column'] = df['column'].astype(float)
✅ 3. String to DateTime
df['Date'] = pd.to_datetime(df['Date'])
✅ 4. Object to Category
df['Category'] = df['Category'].astype('category')
*Useful when column has limited repeating values — reduces memory usage.*
⚠️ *Common Errors*
Converting strings like "abc" to int will cause a crash.
Use pd.to_numeric(df['column'], errors='coerce') to handle this by converting bad values to NaN.
📊 *Real-Life Example:*
In an e-commerce dataset:
- Convert OrderDate to datetime for sorting and time series
- Convert ProductCategory to category to save memory
- Convert Price from string to float before calculating totals
*React with ❤️ once you're ready for the quiz*
Data Science Learning Series: https://whatsapp.com/channel/0029Va8v3eo1NCrQfGMseL2D/998
Python Cheatsheet: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L/1660
❤️
👍
❤
🇮🇳
🇨🇲
🇦🇺
🇵🇰
🇵🇸
♥
❣
58