0% found this document useful (0 votes)
2 views6 pages

Data Wrangling Lab

The document provides a comprehensive guide on data wrangling techniques, including reading various file formats such as CSV, JSON, XML, Excel, and PDF. It outlines practical examples using Python libraries like pandas, PyPDF2, and pdfplumber for data manipulation tasks such as handling missing values, removing duplicates, filtering, sorting, and saving cleaned data. The document serves as a tutorial for effectively managing and processing data in different formats.

Uploaded by

Vanisha Soni
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)
2 views6 pages

Data Wrangling Lab

The document provides a comprehensive guide on data wrangling techniques, including reading various file formats such as CSV, JSON, XML, Excel, and PDF. It outlines practical examples using Python libraries like pandas, PyPDF2, and pdfplumber for data manipulation tasks such as handling missing values, removing duplicates, filtering, sorting, and saving cleaned data. The document serves as a tutorial for effectively managing and processing data in different formats.

Uploaded by

Vanisha Soni
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

Practical Work: Data Wrangling

Reading CSV files, JSON files, XML files , Parsing Excel files , PDF Files

1. Reading CSV files

import csv

with open('[Link]', 'r') as file:

reader = [Link](file)

for row in reader:

print(row)

import pandas as pd

df = pd.read_csv('[Link]')

print([Link]())

2. Reading JSON Data

import json

with open('[Link]', 'r') as file:

data = [Link](file)

print(data)

import pandas as pd

df = pd.read_json('[Link]')

print(df)
3. Reading XML Data

import [Link] as ET

tree = [Link]('[Link]')

root = [Link]()

for child in root:

print([Link], [Link])

Example:

<students>

<student>

<name>Ravi</name>

<age>20</age>

</student>

<student>

<name>Aman</name>

<age>22</age>

</student>

</students>

import pandas as pd

df = pd.read_xml('[Link]')

print(df)
4. Excel Parsing

import pandas as pd

df = pd.read_excel('[Link]')

print(df)

5. PDF Parsing

import PyPDF2

file = open('[Link]', 'rb')

reader = [Link](file)

for page in [Link]:

print(page.extract_text())

import pdfplumber

with [Link]("[Link]") as pdf:

for page in [Link]:

print(page.extract_text())
Program: Data Wrangling on Csv Files

import pandas as pd

Step 1: Read CSV File

df = pd.read_csv('[Link]')

print("Original Data:")

print(df)

print("\n")

Step 2: Basic Information

print("Data Information:")

print([Link]())

print([Link]())

print("\n")

Step 3: Handle Missing Values

print("Missing Values Count:")

print([Link]().sum())

Fill missing Age with mean

df['Age'] = df['Age'].fillna(df['Age'].mean())

print("\nAfter Filling Missing Values:")

print(df)
print("\n")

Step 4: Remove Duplicates

df = df.drop_duplicates()

print("After Removing Duplicates:")

print(df)

print("\n")

Selecting Columns:

print(df['Name'])

print(df[['Name', 'Marks']])

Step 5: Filter Data

print("Students with Marks > 80:")

high_marks = df[df['Marks'] > 80]

print(high_marks)

print("\n")

Step 6: Sort Data

df = df.sort_values(by='Marks', ascending=False)

print("After Sorting by Marks:")

print(df)

print("\n")
Step 7: Rename Column

df = [Link](columns={'Marks': 'Total_Marks'})

print("After Renaming Column:")

print(df)

print("\n")

Step 8: Save Cleaned Data

df.to_csv('cleaned_students.csv', index=False)

print("Cleaned data saved successfully!")

You might also like