Data transformation is a critical process in data manipulation, allowing developers to convert, modify, and reshape data efficiently using Python comprehensions.
1. Mapping and Converting Data Types
## Converting strings to integers
string_numbers = ['1', '2', '3', '4', '5']
integers = [int(num) for num in string_numbers]
## Transforming temperature from Celsius to Fahrenheit
celsius_temps = [0, 10, 20, 30, 40]
fahrenheit_temps = [temp * 9/5 + 32 for temp in celsius_temps]
2. Filtering and Selecting Data
## Filtering out negative numbers
numbers = [-3, -2, -1, 0, 1, 2, 3]
positive_numbers = [num for num in numbers if num > 0]
## Complex filtering with multiple conditions
students = [
{'name': 'Alice', 'grade': 85},
{'name': 'Bob', 'grade': 92},
{'name': 'Charlie', 'grade': 78}
]
high_performers = [
student for student in students
if student['grade'] >= 80
]
graph TD
A[Input Data] --> B[First Transformation]
B --> C[Second Transformation]
C --> D[Final Result]
Example of Nested Comprehension
## Flattening a matrix
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [num for row in matrix for num in row]
## Creating a dictionary with transformed values
words = ['hello', 'world', 'python']
word_lengths = {word: len(word) for word in words}
Technique |
Description |
Example |
Nested Comprehensions |
Create complex transformations |
[x*y for x in range(3) for y in range(3)] |
Conditional Mapping |
Apply transformations with conditions |
[x**2 if x % 2 == 0 else x for x in range(10)] |
Dictionary Comprehensions |
Transform key-value pairs |
{k: v.upper() for k, v in original_dict.items()} |
## Efficient data transformation
import timeit
## Comprehension approach
def transform_with_comprehension():
return [x**2 for x in range(1000) if x % 2 == 0]
## Traditional loop approach
def transform_with_loop():
result = []
for x in range(1000):
if x % 2 == 0:
result.append(x**2)
return result
## Comparing performance
comprehension_time = timeit.timeit(transform_with_comprehension, number=1000)
loop_time = timeit.timeit(transform_with_loop, number=1000)
Learning Tips with LabEx
At LabEx, we emphasize practical skills in data transformation. Practice these techniques to become proficient in Python data manipulation.