pivot_table() function in pandas?pivot_table() function is used to summarize and aggregate data in a DataFrame by creating a new table that groups data by one or more keys and applies aggregation functions like sum, mean, or count.pivot_table()?- data: The DataFrame to summarize.
- index: Column(s) to group by on rows.
- columns: Column(s) to group by on columns.
- values: Column(s) to aggregate.
- aggfunc: Aggregation function like
sum,mean, orcount.
pivot_table() handle missing values by default?pivot_table() fills missing values with NaN. You can change this behavior using the fill_value parameter to replace missing values with a specific value like 0.pivot() and pivot_table()?pivot() reshapes data without aggregation and requires unique index/column pairs. pivot_table() allows aggregation and can handle duplicate entries by applying aggregation functions.pivot_table() to find the average sales per product category.Example:<br><pre>import pandas as pd
data = {'Category': ['A', 'A', 'B', 'B'], 'Sales': [100, 150, 200, 250]}
df = pd.DataFrame(data)
pivot = df.pivot_table(index='Category', values='Sales', aggfunc='mean')
print(pivot)</pre>aggfunc parameter in pivot_table() specify?The aggfunc parameter tells pivot_table() how to combine data, like using sum, mean, or count.
pivot_table() controls the rows of the new table?The index parameter sets which column(s) become the rows in the pivot table.
pivot_table() can handle duplicates by aggregating them, unlike pivot().
pivot_table() fill missing values with by default?By default, missing values are shown as NaN unless fill_value is set.
aggfunc?sort is not an aggregation function; it is used for ordering data.
pivot_table() to summarize sales data by region and product.pivot() and pivot_table() and when to use each.