This can be achieved using the pivot()
function in pandas.
Here is an example:
Suppose you have a DataFrame df
with the following data:
| | name | category | value | | - | -------- | -------- | ----- | | 0 | John | A | 10 | | 1 | John | B | 20 | | 2 | Mary | A | 5 | | 3 | Mary | B | 15 |
To transform the category
column into column headers, you can use the following code:
pivoted = df.pivot(index='name', columns='category', values='value')
This will create a new DataFrame pivoted
with the following data:
| category | A | B | | -------- | -- | -- | | name | | | | John | 10 | 20 | | Mary | 5 | 15 |
Now, the category
column has become the column headers, with the name
column as the index.
Note that if there are multiple values for the same combination of index and column labels, the pivot()
function will aggregate them using a mean by default. You can specify a different aggregation function using the aggfunc
parameter.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-01 09:04:23 +0000
Seen: 8 times
Last updated: Jul 01 '23
How can you display a Pandas Dataframe using a for loop?
What changes need to be made to this for loop to be suitable for use with Pandas, Modin, or Ray?
What does it mean when my time-series code (pandas) shows a ValueError for freq T?
How to calculate the average value of a row in a pandas dataframe that contains NaN and arrays?
How can a single-column be made from a pandas data frame that has n columns?
How can I multiply random elements in my dataframe?
What steps do I need to take to present a pandas DataFrame on a Django template?
How can I create a pandas dataframe using several OR operators in the statement?