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

PP Manual Exp No. 07

The document is a laboratory manual for an experiment at Shri Sant Gajanan Maharaj College of Engineering, focusing on creating a DataFrame from a CSV file using Python. It includes detailed instructions on basic DataFrame operations, data filtering, and sorting, along with example code snippets. The conclusion emphasizes the efficiency of using pandas for data manipulation and analysis.

Uploaded by

Soham Bhole
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 views9 pages

PP Manual Exp No. 07

The document is a laboratory manual for an experiment at Shri Sant Gajanan Maharaj College of Engineering, focusing on creating a DataFrame from a CSV file using Python. It includes detailed instructions on basic DataFrame operations, data filtering, and sorting, along with example code snippets. The conclusion emphasizes the efficiency of using pandas for data manipulation and analysis.

Uploaded by

Soham Bhole
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
You are on page 1/ 9

SHRI SANT GAJANAN MAHARAJ COLLEGE OF ENGG.

LABORATORY MANUAL
SHEGAON - 444 203
PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: Write a program to create a data frame from .csv file and do operations on
it.

EXPERIMENT NO.: SSGMCE/WI/EXTC/003/6ETC08/7 ISSUE NO.: 00 ISSUE DATE: 01.01.2020

REV. DATE:01/01/2024 REV. NO.: 01 DEPTT. : ELECTRONICS & TELECOMMUNICATION ENGG.

LABORATORY PYTHON PROGRAMMING LAB SEMESTER:VI PAGE: 1 OF 3

01 AIM: Write a program to create a data frame from .csv file and do operations on it.
02 APPARATUS/SOFTWARE USED: PC with Python 3.
03 FORMULA: Not applicable

04 THEORY: What are dataframes and CSV files?

Before diving into the details of creating a dataframe from a CSV file, let's first define what a dataframe is
and what a CSV file is.
A dataframe is a two-dimensional, size-mutable, tabular data structure with columns of potentially different
types. It is similar to a spreadsheet or SQL table, and is commonly used to store and manipulate data in
Python.
A CSV (comma-separated values) file, on the other hand, is a plain text file that stores data in a tabular
format, with each row representing a record and each column representing a field. CSV files are a common
way to store data because they are easy to read and write, and can be opened in many different applications,
including Excel and Python.

Program 1: Basic DataFrame Operations


import pandas as pd
This line imports the pandas library, which is used for data manipulation and analysis.

# Read the CSV file into a DataFrame


df = pd.read_csv('example.csv')
This line reads the CSV file named 'example.csv' and loads its data into a pandas DataFrame called df.

# Display the first few rows of the DataFrame


print("DataFrame:")
print(df.head(2))
This section displays the first few rows of the DataFrame ( df) using the head() function. It provides a quick
overview of the dataset's structure and content.
# Check the shape of the DataFrame

print("\nShape of DataFrame:", df.shape)


This line prints the shape of the DataFrame (df), which represents the number of rows and columns in the
dataset. It helps to understand the size and dimensionality of the data.

# Check data types of each column


print("\nData Types:")
print(df.dtypes)
This section prints the data types of each column in the DataFrame (df). Understanding the data types is
essential for performing operations and ensuring data consistency.

# Compute mean of numeric columns


print("\nMean of Numeric Columns:")
print(df.mean())
This line computes the mean (average) of numeric columns in the DataFrame (df) using the mean() function.
It calculates the mean value for each numeric column separately.

# Compute counts of unique values in a categorical column


print("\nValue Counts of a Categorical Column:")
print(df['Category_Column'].value_counts())
This section computes the counts of unique values in a categorical column ('Category_Column') of the
DataFrame (df) using the value_counts() function. It helps to understand the distribution of categorical data.

Program:
import pandas as pd
# Read the CSV file into a DataFrame
df = pd.read_csv('example.csv')

# Display the first few rows of the DataFrame


print("DataFrame:")
print(df.head())

# Check the shape of the DataFrame


print("\nShape of DataFrame:", df.shape)

# Check data types of each column


print("\nData Types:")
print(df.dtypes)

# Compute mean of numeric columns


print("\nMean of Numeric Columns:")
print(df.mean())

# Compute counts of unique values in a categorical column


print("\nValue Counts of a Categorical Column:")
print(df['Category_Column'].value_counts())

Program 2: Data Filtering and Sorting


import pandas as pd
This line imports the pandas library, which is used for data manipulation and analysis.
# Read the CSV file into a DataFrame
df = pd.read_csv('example.csv')
This line reads the CSV file named 'example.csv' and loads its data into a pandas DataFrame called
df.

# Filter the DataFrame based on a condition


filtered_df = df[df['Numeric_Column'] > 50]
This line filters the DataFrame (df) based on a condition. It creates a new DataFrame (filtered_df)
containing only the rows where the value in the 'Numeric_Column' is greater than 50.

# Sort the DataFrame by a specific column


sorted_df = df.sort_values(by='Date_Column')
This line sorts the DataFrame (df) by a specific column ('Date_Column') in ascending order by
default. It creates a new DataFrame (sorted_df) with rows sorted accordingly.

# Display the filtered DataFrame


print("Filtered DataFrame:")
print(filtered_df.head())
This section prints the first few rows of the filtered DataFrame (filtered_df) to display the result
of the filtering operation.

# Display the sorted DataFrame


print("\nSorted DataFrame:")
print(sorted_df.head())
This section prints the first few rows of the sorted DataFrame (sorted_df) to display the result of
the sorting operation.

Program:
import pandas as pd

# Read the CSV file into a DataFrame


df = pd.read_csv('example.csv')

# Filter the DataFrame based on a condition


filtered_df = df[df['Numeric_Column'] > 50]

# Sort the DataFrame by a specific column


sorted_df = df.sort_values(by='Date_Column')

# Display the filtered DataFrame


print("Filtered DataFrame:")
print(filtered_df.head())

# Display the sorted DataFrame


print("\nSorted DataFrame:")
print(sorted_df.head())

Conclusion: In short, using pandas to create a DataFrame from a .sv file enables efficient data manipulation
and analysis. This versatile tool facilitates various operations such as cleaning, transforming, and
visualizing data, empowering users to derive valuable insights and make informed decisions swiftly.

Prepared by: Approved by:

Prof. T.P.Marode/Prof. V.S.Ingole Dr.D.D.Nawgaje

You might also like