Converting to and from a pandas DataFrame
Many of you have used pandas before, especially in your day-to-day work. Although pandas and Polars are often compared as one-or-the-other tools, you can use these tools to supplement each other. Polars allows you to convert between pandas and Polars DataFrames, which is exactly what we’ll cover in this recipe.
Getting ready
You need pandas
and pyarrow
installed for this recipe to work. Execute the following code to make sure that you have them installed:
pip install pandas pyarrow
How to do it...
Here’s how to convert to and from pandas DataFrames. We’ll first create a Polars DataFrame and then go through ways to convert back and forth between Polars and pandas:
- Create a Polars DataFrame from a Python dictionary:
df = pl.DataFrame({ 'a': [1,2,3], 'b': [4,5,6] }) type(df)
The preceding code will return the following output:
>> polars...