Status
Done
5. Quick Inspection & Conversion
Peek at data or convert to NumPy.
df.head(n)
→ Return firstn
rows (default 5).df.tail(n)
→ Return lastn
rows.df.values
→ Return DataFrame values as a NumPy array (legacy).df.to_numpy()
→ Preferred way to convert DataFrame to NumPy array.
Quick Inspection & Conversion in Pandas DataFrames
‣
1. df.head()
- Initial Data Inspection
‣
2. df.tail()
- End Data Inspection
‣
3. df.values
- Legacy NumPy Conversion
‣
4. df.to_numpy()
- Modern NumPy Conversion
Performance & Memory Considerations
Comparison Table
Method | Speed | Memory | Type Preservation | Recommended |
head() /tail() | Very Fast | Minimal | Full | âś… Always |
values | Fast | Variable | Poor | ❌ Legacy |
to_numpy() | Fast | Controlled | Good | âś… Preferred |
Best Practices
- UseÂ
head()
/tail()
 for quick inspection and debugging - PreferÂ
to_numpy()
 overÂvalues
 for conversion - Specify dtypes when usingÂ
to_numpy()
 for better memory usage - Handle missing values explicitly during conversion
Real-World Workflow Example
python
def data_quality_pipeline(df):
"""Professional data inspection and validation workflow"""
# Initial inspection
print("=== DATA QUALITY REPORT ===")
print(f"Shape: {df.shape}")
print("\nFirst 5 rows:")
print(df.head())
print("\nLast 5 rows:")
print(df.tail())
# Type-specific validation
numeric_cols = df.select_dtypes(include='number')
print(f"\nNumeric data preview ({len(numeric_cols.columns)} columns):")
print(numeric_cols.head(3))
# Prepare for analysis
print("\nConverting to NumPy for statistical analysis...")
analysis_data = numeric_cols.to_numpy(dtype='float64')
print(f"Analysis array shape: {analysis_data.shape}")
return analysis_data
# Execute pipeline
analysis_data = data_quality_pipeline(df)
This comprehensive guide shows how these simple methods form the foundation of professional data inspection and conversion workflows in pandas.