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

Map each row into a list and collect all

$
0
0

I have a dataframe:

df = pl.DataFrame(    {"t_left": [0.0, 1.0, 2.0, 3.0],"t_right": [1.0, 2.0, 3.0, 4.0],"counts": [1, 2, 3, 4],    })

And I want to map each row into a list, and then collect all values (to be passed into e.g. matplotlib.hist)

I can do it by hand like:

times = []for t_left, t_right, counts in df.rows():    times.extend(np.linspace(t_left, t_right, counts + 1)[1:])

but this is painfully slow for my data set.

I am completely new to both python and polars, so I was wondering if there is a better way to achieve this.

EDIT

Complete copy-paste example to reproduce.

import polars as plimport numpy as npsize = 1000000df = pl.DataFrame(    {"t_left": np.random.rand(size),"t_right": np.random.rand(size) + 1,"counts": [1] * size,    })times = []for t_left, t_right, counts in df.rows():    times.extend(np.linspace(t_left, t_right, counts + 1)[1:])

Viewing all articles
Browse latest Browse all 23131

Trending Articles



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