Polars Python - Listenversion von Search_Sorted / Filter und in einer Funktion sortierenPython

Python-Programme
Anonymous
 Polars Python - Listenversion von Search_Sorted / Filter und in einer Funktion sortieren

Post by Anonymous »

Ich versuche eine Funktion zu erstellen, die eine DF und filtert und neu arrangiert Zeilen, wobei die Elemente einer angegebenen Spalte c durch eine Liste L ausdrücklich angegeben werden. Der Filteraspekt funktioniert wie erwartet, aber die Sortierung ist nicht das, was ich erwarte. https://docs.pola.rs/api/python/dev/ref ... orted.html
https://docs.pola .rs/api/python/stabil/reference/series/api/polars.series.search_sorted.html

Code: Select all

import polars as pl

df = pl.DataFrame(
{
"a": [1, 2, 3],
"b": [6.0, 5.0, 4.0],
"c": ["z", "x", "y"],
}
)

def filter_sort_explicit(df, c, l):
"""
a function that filters a [df] on [c]olumn by explicitly specificying the order of the values (in that column) in a [list]
"""
return df.filter(pl.col(c).is_in(l)).sort(pl.col(c).search_sorted(l))

for order in ('y x z',
'z x y',
'x z',
'y x',
'z y'):
order = order.split()
print(filter_sort_explicit(df, 'c', order)['c'].to_list() == order)

## should all print true, but that's not the case.

for order in ('y x z',  'z x y',  'x y z'):
order = order.split()
print(order, df.sort(pl.col('c').search_sorted(order, side='left'))['c'].to_list())

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post