Class 12 IP Practical File 2025-26
Class 12 IP Practical File 2025-26
To export a Pandas DataFrame to a CSV file, you use the `to_csv()` method, specifying the file path as a parameter. Considerations include ensuring the correct delimiter is used for your data needs (comma by default), handling indices (which can be included or excluded), and encoding, especially if dealing with non-ASCII data. Choosing appropriate filenames and directories is also important for file organization and accessibility.
To create a Pandas Series using a list, you simply pass the list to the `pd.Series` constructor. This can be useful for tasks such as data preprocessing where you might need to convert a list of data points into a Series to take advantage of Pandas' robust data manipulation capabilities. This allows for operations like filtering, aggregation, and more.
To calculate grades based on marks and add these as a new column, you can use a function to determine the grade from the marks, then apply this function to the DataFrame. A simple example is using Pandas' `apply()` method along with a custom function: `df['grade'] = df['marks'].apply(lambda x: 'A' if x >= 80 else ('B' if x >= 60 else 'C'))`. This code evaluates each row of the 'marks' column and assigns a grade based on predetermined thresholds.
To insert a new student record into a student table, you use the `INSERT INTO` SQL statement. For example: `INSERT INTO students (student_id, name, marks) VALUES ('S004', 'John Doe', 85);`. This query adds a new row to the students table with the specified ID, name, and marks. Such operations are fundamental for updating the dataset with new information.
To construct a pie chart with exploding and shadow effects, you utilize the `matplotlib.pyplot.pie()` method. For example, given a dataset, you can call `plt.pie([20, 30, 50], explode=(0.1, 0, 0.1), shadow=True, labels=['Apple', 'Banana', 'Cherry'])`. This pie chart highlights sections with exploded slices and adds a shadow for a 3D appearance, enhancing the chart’s visual appeal and clarity.
Line charts are typically used to display data trends over intervals or time, often useful in showing growth or change, such as tracking the growth of a plant over time. Bar charts are useful for comparing different groups or categories, for instance, showing marks obtained by a student in multiple subjects. Choosing the correct chart type is crucial for effectively communicating the data insights to the audience.
To handle duplicate rows in a DataFrame, you can use the `drop_duplicates()` method to remove them or the `duplicated()` method to identify them first. For example, `df.drop_duplicates()` removes all duplicate rows based on all columns. Optionally, you can specify the `subset` parameter to consider duplicates only in certain columns. Handling duplicates is crucial for ensuring data integrity and avoiding skewed analysis results.
Concatenating two 2D arrays involves using functions such as `numpy.concatenate()` where you specify the arrays and the axis along which to concatenate. For example, `np.concatenate((array1, array2), axis=0)` will join the arrays along rows. Potential challenges include ensuring compatible shapes along the concatenation axis and managing memory efficiently for large arrays. Such operations are essential for data integration and complex data manipulations.
To group data in a Pandas DataFrame and perform an aggregation, you use the `groupby()` method followed by an aggregation function like `sum()`. For instance, to sum expenditures by category, you would call `df.groupby('category')['expenditure'].sum()`, where 'category' is the column to group by and 'expenditure' is the column to aggregate. This technique is particularly useful for summarizing data and gaining insights into different segments.
To retrieve records of students who scored above 80 marks, you employ the `SELECT` statement with a `WHERE` clause in SQL, such as: `SELECT * FROM students WHERE marks > 80;`. This command filters students whose marks exceed 80, allowing for targeted analysis or reporting on high-performing students.