Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Polars - Flatten rows into columns, aggregating by column values

$
0
0

I'm trying to write a script in Polars that would flatten a list of prices per date and minute. The catch is I want to incrementally aggregate into columns and zero out values in the future. For example. Idea is to make this solution vectorized if possible to make it performant.

df = pl.DataFrame({"date": ["2022-01-01", "2022-01-01", "2022-01-02", "2022-01-02", "2022-01-02", "2022-01-03", "2022-01-03", "2022-01-03"],"minute": [1, 2, 1, 2, 3, 1, 2, 3],"price": [10, 20, 15, 10, 20, 30, 60, 70]})

Should build the following dataframe.

-------------------------------------------------------| date         | minute | 1_price | 2_price | 3_price  -------------------------------------------------------| "2022-01-01" | 1      | 10      | 0       | 0| "2022-01-01" | 2      | 10      | 20      | 0| "2022-01-02" | 1      | 15      | 0       | 0| "2022-01-02" | 2      | 15      | 10      | 0| "2022-01-02" | 3      | 15      | 10      | 20| "2022-01-03" | 1      | 30      | 0       | 0| "2022-01-03" | 2      | 30      | 60      | 0| "2022-01-03" | 3      | 30      | 60      | 70

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>