[go: up one dir, main page]

0% found this document useful (0 votes)
42 views2 pages

Case Based Questions DATA - CSV FILE

The document outlines tasks for working with CSV files using Pandas in Python, including creating DataFrames from 'student.csv', 'staff.csv', and 'Emp.csv'. It provides sample code for reading CSV files, displaying their contents, and manipulating column headers. Additionally, it includes tips for ensuring proper CSV formatting and file management.

Uploaded by

hrshvrdhnpradhan
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)
42 views2 pages

Case Based Questions DATA - CSV FILE

The document outlines tasks for working with CSV files using Pandas in Python, including creating DataFrames from 'student.csv', 'staff.csv', and 'Emp.csv'. It provides sample code for reading CSV files, displaying their contents, and manipulating column headers. Additionally, it includes tips for ensuring proper CSV formatting and file management.

Uploaded by

hrshvrdhnpradhan
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/ 2

Case-Based Question: Working with CSV Files using Pandas

�Case 1: student.csv
�Task Description:
Create a CSV file named student.csv and perform the following tasks:
a.) Read the CSV file and create a DataFrame for it.
b.) Display the contents of the DataFrame.
c.) Create another DataFrame from the same file but the column headers should appear as 0, 1,
2... (ignoring the original column headers).
Sample Code:
import pandas as pd

# a & b. Read CSV and display contents


Df1 = pd.read_csv("student.csv")
print("Original DataFrame:")
print(Df1)

# c. Read the file with no headers


df2 = pd.read_csv("student.csv", header=None)
print("\nDataFrame with default numeric column headers:")
print(df2)

Case 2: staff.csv
Task Description:
Create a CSV file named staff.csv and perform the following:
a.) Read the file and create a DataFrame.
b.) Create another DataFrame where the column headers should be S_id, S_name, S_dept,
S_salary, replacing original headers.
c.) Display the highest salary from the DataFrame.
Sample Code:
import pandas as pd

# a. Read the CSV file


Df1 = pd.read_csv("staff.csv")
print("Original DataFrame:")
print(Df1)

# b. Custom column names and skipping original header


df2 = pd.read_csv("staff.csv", names=["S_id", "S_name", "S_dept",
"S_salary"], skiprows=1)
print("\nCustom Header DataFrame:")
print(df2)

# c. Display highest salary


print("\nHighest Salary:", df2["S_salary"].max())
Case 3: Emp.csv
Task Description:
Create a CSV file named Emp.csv with at least 10 rows and perform the following tasks:
a.) Read the CSV file and create a DataFrame.
b.) Display the first 5 rows.
c.) Display the 3rd, 4th, and 5th rows of the file.

Sample Code:
import pandas as pd

# a. Read the CSV file


Df1 = pd.read_csv("Emp.csv")
print("Original DataFrame:")
print(Df1)

# b. Display first 5 rows


df2 = pd.read_csv("Emp.csv", nrows=5)
print("\nFirst 5 Rows:")
print(df2)

# c. Display 3rd, 4th, 5th rows (index 2 to 4)


print("\n3rd to 5th Rows:")
print(df2[2:5])

Tips for Students:


 Ensure your CSV files are properly formatted with commas separating values.
 Save your .csv files in the same directory as your Python script (or provide full path).
 Use print(df.head()) or print(df.tail()) for previewing data.

You might also like