Reading and writing multiple files
When working on actual data projects, there are cases where data is split into multiple files in a directory. Dealing with each file one by one can be a pain and may distract you from working on other critical components of your project.
In this recipe, we’ll cover reading multiple files into a single DataFrame or into multiple DataFrames, as well as writing a DataFrame to multiple files.
How to do it...
Here are some ways to work with multiple files:
- Write a DataFrame to multiple CSV files:
- Create a DataFrame:
data = {'Letter': ['A','B','C'], 'Value': [1,2,3]} df = pl.DataFrame(data)
- Split it into multiple DataFrames:
dfs = df.group_by(['Letter']) print(dfs)
The preceding code will return the following output:
>> <polars.dataframe.group_by.GroupBy object at 0x154373390>
- Write them to CSV files:
for name, df in dfs: df.write_csv(f'...