Pandas_Matplotlib_QA class 12
Pandas_Matplotlib_QA class 12
Pandas is an open-source Python library used for data manipulation and analysis. The two main
structure. Example:
Series:
DataFrame:
3. Write Python code to create a Series from a list, a dictionary, and a NumPy array.
From list:
pd.Series([1, 2, 3])
From dictionary:
pd.Series(np.array([4, 5, 6]))
head() shows the first 5 rows; tail() shows the last 5 rows of the DataFrame or Series.
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
Addition: s1 + s2
Subtraction: s1 - s2
df = pd.DataFrame(data)
11. What are the different ways to read data into a DataFrame in Pandas?
13. How can you display only specific columns from a DataFrame?
It returns summary statistics (count, mean, std, min, max, etc.) for numeric columns.
15. Write a Python program to read a CSV file and display basic statistics.
df = pd.read_csv('data.csv')
print(df.describe())
df.sort_values(by='column_name', ascending=True)
Example: df.groupby('column').mean()
drop() removes columns/rows and returns a new object. del deletes a column in-place.
Matplotlib is a Python library for creating static, interactive, and animated plots.
plt.show()
26. Write a program to create a bar chart for student marks in 5 subjects.
subjects = ['Math', 'Sci', 'Eng', 'Hist', 'Geo']
plt.bar(subjects, marks)
plt.show()
27. How can you plot multiple lines on the same graph?
x, y: data points; label: legend label; color: line color; linestyle: style of line etc.
30. Write a program to create a pie chart showing percentage distribution of expenses.
plt.show()