Issue
I want to convert the values of my dataframe into an array using Polars.
With Pandas I would do this:
import pandas as pd
df_tests = pd.DataFrame({'col_a':[1,2,3], 'col_b':[4,5,6]}, 'col_c':[7,8,9]})
print(df_tests.values)
What's the equivalent in Polars?
I've tried the to_list() method, but only works for series. The closest I could get is this, which returns a list of tuples:
import polars as pl
# Convert Pandas DataFrame to Polars DataFrame
df_tests = pd.DataFrame({'col_a':[1,2,3], 'col_b':[4,5,6]}, 'col_c':[7,8,9]})
df_tests_pl = pl.from_pandas(df_tests)
print(df_tests_pl.select(pl.col(['col_a', 'col_b'])).rows())
Solution
If you're looking for the values of a DataFrame as a numpy array, use to_numpy
:
import polars as pl
df = pl.DataFrame({"col_a": [1, 2, 3], "col_b": [4, 5, 6], "col_c": [7, 8, 9]})
df.to_numpy()
# [[1 4 7]
# [2 5 8]
# [3 6 9]]
If you want the row values as Python objects, you should be using the rows
method, which will give you a list of tuples:
df.rows()
# [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
However, Polars likes columnar data. If you're looking for the values in a DataFrame, you should ideally be using to_dict
, as it is much more efficient:
df.to_dict(as_series=False)
# {'col_a': [1, 2, 3], 'col_b': [4, 5, 6], 'col_c': [7, 8, 9]}
Answered By - stinodego
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.