## How to pivot a pandas Dataframe in Python?

pandas pivot multiple columns

pandas pivot multiindex

valueerror: index contains duplicate entries, cannot reshape

pandas melt

pandas pivot vs pivot_table

pandas pivot table column names

pandas pivot count

**This question already has answers here**:

You just add .T :) df.describe().T to transpose your results:

import pandas as pd import numpy as np #Create a Dictionary of series d = {'Name':pd.Series(['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine', 'Rahul','David','Andrew','Ajay','Teresa']), 'Age':pd.Series([26,27,25,24,31,27,25,33,42,32,51,47]), 'Score':pd.Series([89,87,67,55,47,72,76,79,44,92,99,69])} #Create a DataFrame pd.DataFrame(d).describe().T

Results:

**pandas.DataFrame.pivot,** Reshape data (produce a “pivot” table) based on column values. Uses unique values from index / columns to form axes of the resulting DataFrame. pandas.DataFrame.pivot¶ DataFrame.pivot (self, index = None, columns = None, values = None) → ’DataFrame’ [source] ¶ Return reshaped DataFrame organized by given index / column values. Reshape data (produce a “pivot” table) based on column values. Uses unique values from specified index / columns to form axes of the resulting DataFrame. This function does not support data aggregation, multiple values will result in a MultiIndex in the columns.

You can `transpose`

the dataframe:

`data_pivot = data_pd.T`

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.transpose.html

**pandas.DataFrame.pivot_table,** DataFrame. pivot_table (self, values=None, index=None, columns=None, The levels in the pivot table will be stored in MultiIndex objects (hierarchical pandas.pivot(index, columns, values) function produces pivot table based on 3 columns of the DataFrame. Uses unique values from index / columns and fills with values. Parameters: index[ndarray] : Labels to use to make new frame’s index

Other way is `.transpose()`

:

data_pivot = data_pd.transpose()

**Reshaping and pivot tables,** To reshape the data into this form, we use the DataFrame.pivot() method (also The following numpy.unique will fail under Python 3 with a TypeError because pandas pivot table to data frame. In this question, the OP is concerned with the output of the pivot. Namely how the columns look. OP wanted it to look like R. This isn't very helpful for pandas users. pandas pivoting a dataframe, duplicate rows. Another decent question but the answer focuses on one method, namely pd.DataFrame.pivot

**Python,** pandas.pivot(index, columns, values) function produces pivot table based on 3 importing pandas as pd. import pandas as pd. # creating a dataframe. df = pd. Creating a Pivot Table in Pandas To get started with creating a pivot table in Pandas, let’s build a very simple pivot table to start things off. We’ll begin by aggregating the Sales values by the Region the sale took place in: sales_by_region = pd.pivot_table(df, index = 'Region', values = 'Sales')

**Pandas DataFrame: pivot() function,** The pivot() function is used to reshaped a given DataFrame organized by given index / column values. This function does not support data Create pivot table in Pandas python with aggregate function sum: # pivot table using aggregate function sum pd.pivot_table(df, index=['Name','Subject'], aggfunc='sum') So the pivot table with aggregate function sum will be. Which shows the sum of scores of students across subjects . Create pivot table in Pandas python with aggregate function count:

**How to pivot a dataframe in Pandas?,** How to pivot a dataframe in Pandas? [duplicate] · python pandas dataframe transpose. This question already has an answer here: How to Next, you’ll see how to pivot the data based on those 5 scenarios. 5 Scenarios of Pivot Tables in Python using Pandas Scenario 1: Total sales per employee. To get the total sales per employee, you’ll need to add the following syntax to the Python code: pivot = df.pivot_table(index=['Name of Employee'], values=['Sales'], aggfunc='sum')

##### Comments

- What you're doing is not exactly a pivot; you are transposing the table. There are already answers for that.