ASSIGNMENT

ASSIGNMENT

Q.8 Write a Python script to create a data frame using list in python.

Answer :- 

 

import pandas as pd
# Create an empty list to store student data
students = []
# Get the number of students from the user
num_students = int(input("Enter the number of students: "))
# Loop to collect data for each student
for i in range(num_students):
    print(f"\nEnter details for Student {i + 1}:")
    sid = input("Enter Student ID: ")
    sname = input("Enter Student Name: ")
    sub1 = float(input("Enter marks for Subject 1: "))
    sub2 = float(input("Enter marks for Subject 2: "))
    sub3 = float(input("Enter marks for Subject 3: "))
    sub4 = float(input("Enter marks for Subject 4: "))
    sub5 = float(input("Enter marks for Subject 5: "))
    sub6 = float(input("Enter marks for Subject 6: "))
 # Calculate total and percentage
    total = sub1 + sub2 + sub3 + sub4 + sub5 + sub6
    per = total / 6  # Assuming each subject has equal weight
# Append the data to the students list
    students.append([sid, sname, sub1, sub2, sub3, sub4, sub5, sub6, total, per])
# Define column names for the DataFrame
columns = ["SID", "SName", "Sub1", "Sub2", "Sub3", "Sub4", "Sub5", "Sub6", "Total", "Percentage"]
# Create the DataFrame
df = pd.DataFrame(students, columns=columns)
# Display the DataFrame
print("\nStudent DataFrame:")
print(df)
    

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.