OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to get a mean of a list of columns in a polars dataframe

  • Thread starter Thread starter Paul Fleming
  • Start date Start date
P

Paul Fleming

Guest
I want to get the average of a list of columns within a polars dataframe, but am getting stuck. For example:

Code:
df = pl.DataFrame({
    'a':[1,2,3],
    'b':[4,5,6],
    'c':[7,8,9]
})

cols_to_mean = ['a','c']

This works:

Code:
df.select(pl.col(cols_to_mean))

In that it returns just those columns, but when I try to calculate the mean, this line

Code:
df.select(pl.col(cols_to_mean).mean())

Returns the mean of each column (while I want a column the same length as each that is the mean of them both for each row). There isn't an option to pass an axis to the mean function. I also try:

Code:
df.select(pl.mean(pl.col(cols_to_mean).mean()))

But this produces an error:

Code:
TypeError: Invalid input for `col`. Expected `str` or `DataType`, got

Is there a way to do this?
<p>I want to get the average of a list of columns within a polars dataframe, but am getting stuck. For example:</p>
<pre><code>df = pl.DataFrame({
'a':[1,2,3],
'b':[4,5,6],
'c':[7,8,9]
})

cols_to_mean = ['a','c']
</code></pre>
<p>This works:</p>
<pre><code>df.select(pl.col(cols_to_mean))
</code></pre>
<p>In that it returns just those columns, but when I try to calculate the mean, this line</p>
<pre><code>df.select(pl.col(cols_to_mean).mean())
</code></pre>
<p>Returns the mean of each column (while I want a column the same length as each that is the mean of them both for each row). There isn't an option to pass an axis to the mean function. I also try:</p>
<pre><code>df.select(pl.mean(pl.col(cols_to_mean).mean()))
</code></pre>
<p>But this produces an error:</p>
<pre><code>TypeError: Invalid input for `col`. Expected `str` or `DataType`, got
</code></pre>
<p>Is there a way to do this?</p>
 

Latest posts

Top