Ask Your Question
0

How can column values be transformed into column headers using pandas?

asked 2023-07-01 09:04:23 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-01 09:26:02 +0000

plato gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-07-01 09:04:23 +0000

Seen: 8 times

Last updated: Jul 01 '23