Ask Your Question
3

How can one partition a dataframe in python using polars and then pivot and concatenate it?

asked 2022-03-07 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-07-21 19:00:00 +0000

scrum gravatar image

To partition a dataframe in Polars, you can use the groupby method, which groups the rows based on one or more columns.

Here's an example code snippet:

import polars as pl

df = pl.DataFrame({
    'day': [1, 1, 2, 2, 3, 3],
    'category': ['A', 'B', 'A', 'B', 'A', 'B'],
    'value': [10, 20, 30, 40, 50, 60]
})

# group by day and category
gb = df.groupby(['day', 'category'])

# aggregate the values column by sum
aggregated = gb.agg({'value': 'sum'})

# pivot the table
pivoted = aggregated.pivot(index='day', columns='category')

# concatenate the two columns
result = pl.concat([pivoted['value']['A'], pivoted['value']['B']], axis=1)

In this example, we have a dataframe df with columns day, category, and value. We group the rows by day and category, and aggregate the value column by summing it up.

Then, we pivot the resulting table by specifying the index and columns, and finally, concatenate the two columns A and B using the concat function.

Note that we accessed the value column in the pivoted table using pivoted['value']['A'] and pivoted['value']['B']. The first index value is the name of the aggregated column, and the second index A and B are the values of the category column.

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: 2022-03-07 11:00:00 +0000

Seen: 8 times

Last updated: Jul 21 '21