Hslip28
Q1]
Assuming the file "student.dat" has the following structure: rollno, name, OS, WT, DS, Python, Java, CN
file_path = "student.dat"
with open(file_path, 'r') as file:
lines = file.readlines()
Displaying data in tabular format
print("Roll No\tName\tOS\tWT\tDS\tPython\tJava\tCN\tPercentage")
for line in lines:
data = line.strip().split(',')
rollno, name, OS, WT, DS, Python, Java, CN = map(float, data[0:8])
percentage = (OS + WT + DS + Python + Java + CN) / 6
print(f"{int(rollno)}\t{name}\t{OS}\t{WT}\t{DS}\t{Python}\t{Java}\t{CN}\t{percentage:.2f}%")
Q2]
import pandas as pd
import numpy as np
1. Create a dataframe with columns name, age, and percentage
data = {'name': ['John', 'Alice', 'Bob', 'Charlie', 'Emma', 'David', 'Eva', 'Frank', 'Grace', 'Henry'],
'age': [22, 25, 23, 22, 24, 26, 23, 27, 25, 28],
'percentage': [75.2, 82.5, 67.8, 90.5, 78.3, 88.9, 76.4, 94.2, 81.7, 89.6]}
df = pd.DataFrame(data)
2. Print shape, number of rows-columns, data types, feature names, and data description
print("DataFrame Information:")
print("Shape:", df.shape)
print("Number of Rows-Columns:", df.shape[0], "-", df.shape[1])
print("Data Types:")
print(df.dtypes)
print("\nFeature Names:")
print(df.columns)
print("\nData Description:")
print(df.describe())
3. View basic statistical details of the data
print("\nBasic Statistical Details:")
print(df.describe())
4. Add 5 rows with duplicate values and missing values, add a column 'remarks' with empty values
additional_data = {'name': ['John', 'Alice', 'Bob', 'Charlie', 'Emma'],
'age': [22, 25, 23, 22, 24],
'percentage': [75.2, np.nan, 67.8, 90.5, 78.3],
'remarks': ['', '', '', '', '']}
df = pd.concat([df, pd.DataFrame(additional_data)], ignore_index=True)
Display the modified data
print("\nModified DataFrame:")
print(df)