0% found this document useful (0 votes)
9 views4 pages

Data Cleaning Techniques in Python

Uploaded by

bsf23000703
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Data Cleaning Techniques in Python

Uploaded by

bsf23000703
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Handling Missing Values

Operation: Identify columns with missing values and assess the extent of missingness.

Python Functions:

# Checking for missing values

[Link]().sum()

# Fill missing values with median

df['mean_cpu_usage_rate'].fillna(df['mean_cpu_usage_rate'].median(), inplace=True)

# Drop rows or columns with too many missing values

[Link](axis=0, thresh=5) # Keep rows with at least 5 non-NaN values

2. Removing Duplicate Entries

Operation: Check for and remove duplicate rows.

Python Functions: *

python

# Identifying duplicates

duplicates = df[[Link]()]

# Removing duplicates

df.drop_duplicates(inplace=True)

3. Correcting Data Types

Operation: Ensure that columns have the correct data types.

Python Functions:

# Convert column to float

df['mean_cpu_usage_rate'] = df['mean_cpu_usage_rate'].astype(float)
# Convert to datetime

df['start_time'] = pd.to_datetime(df['start_time'])

df['end_time'] = pd.to_datetime(df['end_time'])

4. Filtering Outliers

Operation: Detect and manage outliers using statistical techniques.

Python Functions:

# Using Z-score to identify outliers

from [Link] import zscore

df['zscore'] = zscore(df['mean_cpu_usage_rate'])

outliers = df[(df['zscore'] < -3) | (df['zscore'] > 3)]

# Removing outliers

df = df[(df['zscore'] >= -3) & (df['zscore'] <= 3)]

```

5. Standardizing Units and Scales

Operation: Ensure all measurements are in consistent units and scales.

Python Functions:

python

# Convert bytes to megabytes

df['assigned_memory_usage_MB'] = df['assigned_memory_usage'] / (1024 * 1024)

# Normalize or scale data

from [Link] import MinMaxScaler

scaler = MinMaxScaler()
df[['mean_cpu_usage_rate', 'assigned_memory_usage_MB']] = scaler.fit_transform(

df[['mean_cpu_usage_rate', 'assigned_memory_usage_MB']]

6. Handling Inconsistent Entries

Operation: Clean up inconsistencies in the data.

Python Functions:

# Correct inconsistent entries

df['aggregation_type'] = df['aggregation_type'].[Link]().replace(

{'sum': 'sum', 'SUM': 'sum', 'Summation': 'sum'}

7. Correcting Timestamp Misalignments

Operation: Ensure proper alignment of `start_time` and `end_time`.

Python Functions:

# Find rows where end_time is before start_time

misaligned = df[df['end_time'] < df['start_time']]

# Fix or drop these rows as necessary

df = df[df['end_time'] >= df['start_time']]

8. Removing Irrelevant Columns

Operation: Drop columns that are not needed for analysis.

Python Functions:

# Drop unnecessary columns

[Link](['sample_portion', 'aggregation_type'], axis=1, inplace=True)

```
9. Consistent Handling of Zero or Negative Values

Operation: Identify and handle zero or negative values appropriately.

Python Functions:

# Replace negative or zero values with NaN and then handle them

df['mean_cpu_usage_rate'] = df['mean_cpu_usage_rate'].replace(

lambda x: x if x > 0 else None

df['mean_cpu_usage_rate'].fillna(df['mean_cpu_usage_rate'].median(), inplace=True)

10. Data Sampling and Reduction

Operation: Reduce dataset size without losing critical information.

Python Functions:

# Random sampling of data

sampled_df = [Link](frac=0.1, random_state=42) # Take 10% sample

# Aggregating data to hourly means

df['hourly_time'] = df['start_time'].[Link]('H')

aggregated_df = [Link]('hourly_time').agg({

'mean_cpu_usage_rate': 'mean',

'assigned_memory_usage_MB': 'sum'

}).reset_index()

```

By following these steps and utilizing the corresponding Python functions, you can effectively clean the
Google Cluster Dataset, preparing it for further analysis and ensuring that the insights you derive will be
reliable and accurate.

Common questions

Powered by AI

Removing irrelevant columns reduces the dimensionality of the dataset, which simplifies analysis, speeds up computation, and can enhance model performance by eliminating noise. This can be achieved by assessing the relevance of columns to the analysis goals and discarding those like df.drop(['sample_portion', 'aggregation_type'], axis=1). Columns with no influence or contribution to the objectives should be considered for removal to focus on actionable insights and retain only significant predictors .

Zero or negative values, particularly in contexts where they are meaningless, can introduce errors or distort analytical models. These values can be replaced with NaN to be managed appropriately, such as with df['mean_cpu_usage_rate'].replace(lambda x: x if x > 0 else None). After handling, fill missing data with statistical options like the median (df['mean_cpu_usage_rate'].fillna(df['mean_cpu_usage_rate'].median())), ensuring data analysis remains credible and interpretation valid .

Outliers can significantly skew statistical analyses and modeling outcomes. Detecting and removing them ensures that the analysis reflects the true pattern of the dataset. The Z-score method, implemented through from scipy.stats import zscore, identifies values that deviate significantly from the mean (using df['zscore'] = zscore(df['mean_cpu_usage_rate'])). Data points with Z-scores below -3 or above 3 can be considered outliers and filtered out, thus enhancing analytical accuracy by focusing on the majority data trends .

Handling missing values can be approached by identifying the columns with missing values using functions like df.isnull().sum() and assessing the extent of missingness. Missing values can be filled with the median of the column using the fillna() function to ensure that the dataset remains statistically unbiased. Alternatively, rows or columns with excessive missing values can be dropped using df.dropna(axis=0, thresh=5), where only rows with at least 5 non-NaN values are retained. These methods improve data quality by minimizing biases and ensuring completeness in dataset columns .

Standardizing units and scales ensures that features are on a uniform scale, which is crucial for models that are sensitive to the distribution of input data, such as those that rely on Euclidean distances. In Python, unit conversion can be done by mathematical operations (e.g., converting bytes to megabytes with df['assigned_memory_usage_MB'] = df['assigned_memory_usage'] / (1024 * 1024)), while scaling is done using the MinMaxScaler from sklearn.preprocessing, fitting and transforming data to a common scale .

Sampling and aggregating data helps in managing large datasets by reducing their complexity and size while retaining key statistical properties. Random sampling, such as df.sample(frac=0.1, random_state=42), captures representative subsets of data. Aggregating, for example by hourly means using df.groupby('hourly_time').agg(), can compress temporal data into consistent time intervals, preserving crucial insights without analyzing every data point individually. These processes contribute to efficient data handling and clearer insights .

Removing duplicates is crucial in scenarios where data redundancy can mislead analytical results, such as repeated transaction entries inflating totals or affecting averages. In Python, duplicates are found using df[df.duplicated()] and removed with df.drop_duplicates(inplace=True). This facilitates clearer, more accurate data insights by ensuring each entry contributes uniquely to the dataset and eliminating potential biases from repeated records .

Data type conversion ensures that each column stores data in its appropriate format, which is critical for precise calculations and operations. In Python, the astype() function is used to convert columns to different types, such as df['mean_cpu_usage_rate'].astype(float) for numerical data, while pd.to_datetime() converts date strings to datetime objects, ensuring compatibility and enabling time-based operations .

Handling inconsistent categorical entries involves standardizing unique representations of the same entity, which avoids redundant or erroneous analysis. This can be done by converting strings to a consistent format and replacing variations with a common term, such as using df['aggregation_type'].replace({'sum': 'sum', 'SUM': 'sum', 'Summation': 'sum'}) after converting text to lowercase. Consistency is crucial for accurate data aggregation and interpretation, reducing noise from categorical disparities .

Proper timestamp alignment ensures temporal data integrity, enabling accurate time-based analytics such as duration calculations and time series analysis. Rows where end_time precedes start_time represent misalignments and can distort analyses. These misaligned timestamps can be identified and rectified by filtering out using conditions like df[df['end_time'] >= df['start_time']]. This action aids in maintaining logical consistency within the dataset .

You might also like