53. Data Transformation with Pandas

1. Loading Data from CSV

Loading data from a CSV file into a Pandas DataFrame.

Copy

import pandas as pd

# Load data from a CSV file
df = pd.read_csv('data.csv')

# Display first 5 rows of the dataframe
print(df.head())

This code loads data from a CSV file named data.csv and displays the first 5 rows.


2. Data Cleaning: Handling Missing Values

Filling missing values with a specified value.

Copy

import pandas as pd

# Sample DataFrame with missing values
data = {'Name': ['Alice', 'Bob', 'Charlie', None], 'Age': [25, None, 30, 22]}
df = pd.DataFrame(data)

# Fill missing values with a default value
df['Age'] = df['Age'].fillna(df['Age'].mean())

# Display cleaned DataFrame
print(df)

This example demonstrates how to fill missing values in the Age column with the mean of the column.


3. Dropping Rows with Missing Values

Removing rows that have missing values.

Copy

This snippet removes any rows with missing data.


4. Converting Data Types

Changing the data type of a column.

Copy

This code converts the Price column from strings to floating-point numbers.


5. Renaming Columns

Renaming columns in a DataFrame.

Copy

This snippet demonstrates how to rename columns in the DataFrame.


6. Filtering Data Based on Conditions

Filtering rows based on a condition.

Copy

This example filters the DataFrame to show only rows where the Age column is greater than 30.


7. Sorting Data

Sorting data by one or more columns.

Copy

This snippet sorts the DataFrame by the Age column in ascending order.


8. Grouping Data

Grouping data and performing aggregation operations.

Copy

This example groups data by the Category column and calculates the sum of the Value column for each group.


9. Applying Functions to Columns

Applying a custom function to a DataFrame column.

Copy

This snippet demonstrates how to apply a lambda function to a column (Age) to compute a new column (Age in 10 years).


10. Concatenating DataFrames

Concatenating multiple DataFrames along rows or columns.

Copy

This code concatenates two DataFrames df1 and df2 along the rows, combining them into a single DataFrame.


These snippets cover various common data manipulation tasks in Pandas, such as loading, cleaning, filtering, grouping, and transforming data. You can combine these techniques to perform more complex data analysis and transformation tasks.

Last updated