Quickstart In this quickstart, you will learn the basics of Daft's DataFrame and SQL API and the features that set it apart from frameworks like Pandas, PySpark, Dask, and Ray.
Install Daft You can install Daft using pip
. Run the following command in your terminal or notebook:
For more advanced installation options, please see Installation .
Create Your First DataFrame in Daft See also I/O API Docs . Let's create a DataFrame from a dictionary of columns:
๐ Python
import daft
df = daft . from_pydict ({
"A" : [ 1 , 2 , 3 , 4 ],
"B" : [ 1.5 , 2.5 , 3.5 , 4.5 ],
"C" : [ True , True , False , False ],
"D" : [ None , None , None , None ],
})
df
Output 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 โญโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโฎ
โ A โ B โ C โ D โ
โ --- โ --- โ --- โ --- โ
โ Int64 โ Float64 โ Boolean โ Null โ
โโโโโโโโโชโโโโโโโโโโชโโโโโโโโโโชโโโโโโโก
โ 1 โ 1.5 โ true โ None โ
โโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโค
โ 2 โ 2.5 โ true โ None โ
โโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโค
โ 3 โ 3.5 โ false โ None โ
โโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโค
โ 4 โ 4.5 โ false โ None โ
โฐโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโฏ
(Showing first 4 of 4 rows)
You just created your first DataFrame!
Read From a Data Source Daft supports both local paths as well as paths to object storage such as AWS S3:
Note
To work with other formats like Delta Lake and Iceberg , check out their respective pages.
Letโs read in a Parquet file from a public S3 bucket. Note that this Parquet file is partitioned on the column country
. This will be important later on.
Output โญโโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฎ
โ first_name โ last_name โ age โ DoB โ country โ has_dog โ
โ --- โ --- โ --- โ --- โ --- โ --- โ
โ Utf8 โ Utf8 โ Int64 โ Date โ Utf8 โ Boolean โ
โฐโโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโดโโโโโโโดโโโโโโโโโโดโโโโโโโโโโฏ
(No data to display: Dataframe not materialized)
Why does it say (No data to display: Dataframe not materialized)
and where are the rows?
Execute Your DataFrame and View Data Daft is lazy by default. This means that the contents will not be computed (โmaterializedโ) unless you explicitly tell Daft to do so. This is best practice for working with larger-than-memory datasets and parallel/distributed architectures.
The file we have just loaded only has 5 rows. You can materialize the whole DataFrame in memory easily using the df.collect()
method:
Output 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 โญโโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโฎ
โ first_name โ last_name โ age โ DoB โ country โ has_dog โ
โ --- โ --- โ --- โ --- โ --- โ --- โ
โ Utf8 โ Utf8 โ Int64 โ Date โ Utf8 โ Boolean โ
โโโโโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโโโชโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโชโโโโโโโโโโก
โ Shandra โ Shamas โ 57 โ 1967-01-02 โ United Kingdom โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Zaya โ Zaphora โ 40 โ 1984-04-07 โ United Kingdom โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Wolfgang โ Winter โ 23 โ 2001-02-12 โ Germany โ None โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Ernesto โ Evergreen โ 34 โ 1990-04-03 โ Canada โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ James โ Jale โ 62 โ 1962-03-24 โ Canada โ true โ
โฐโโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโโฏ
(Showing first 5 of 5 rows)
To view just the first few rows, you can use the df.show()
method:
Output 1
2
3
4
5
6
7
8
9
10
11
12
13 โญโโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโฎ
โ first_name โ last_name โ age โ DoB โ country โ has_dog โ
โ --- โ --- โ --- โ --- โ --- โ --- โ
โ Utf8 โ Utf8 โ Int64 โ Date โ Utf8 โ Boolean โ
โโโโโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโโโชโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโชโโโโโโโโโโก
โ Shandra โ Shamas โ 57 โ 1967-01-02 โ United Kingdom โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Zaya โ Zaphora โ 40 โ 1984-04-07 โ United Kingdom โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Wolfgang โ Winter โ 23 โ 2001-02-12 โ Germany โ None โ
โฐโโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโโฏ
(Showing first 3 of 5 rows)
Now let's take a look at some common DataFrame operations.
Select Columns You can select specific columns from your DataFrame with the df.select()
method:
Output 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 โญโโโโโโโโโโโโโฌโโโโโโโโโโฎ
โ first_name โ has_dog โ
โ --- โ --- โ
โ Utf8 โ Boolean โ
โโโโโโโโโโโโโโชโโโโโโโโโโก
โ Shandra โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโค
โ Zaya โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโค
โ Ernesto โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโค
โ James โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโค
โ Wolfgang โ None โ
โฐโโโโโโโโโโโโโดโโโโโโโโโโฏ
(Showing first 5 of 5 rows)
Select Rows You can filter rows using the df.where()
method that takes an Logical Expression predicate input. In this case, we call the df.col()
method that refers to the column with the provided name age
:
Output 1
2
3
4
5
6
7
8
9
10
11
12
13 โญโโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโฎ
โ first_name โ last_name โ age โ DoB โ country โ has_dog โ
โ --- โ --- โ --- โ --- โ --- โ --- โ
โ Utf8 โ Utf8 โ Int64 โ Date โ Utf8 โ Boolean โ
โโโโโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโโโชโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโชโโโโโโโโโโก
โ Shandra โ Shamas โ 57 โ 1967-01-02 โ United Kingdom โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Zaya โ Zaphora โ 40 โ 1984-04-07 โ United Kingdom โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ James โ Jale โ 62 โ 1962-03-24 โ Canada โ true โ
โฐโโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโโฏ
(Showing first 3 of 3 rows)
Filtering can give you powerful optimization when you are working with partitioned files or tables. Daft will use the predicate to read only the necessary partitions, skipping any data that is not relevant.
Note
As mentioned earlier that our Parquet file is partitioned on the country
column, this means that queries with a country
predicate will benefit from query optimization.
Exclude Data You can limit the number of rows in a DataFrame by calling the [df.limit()
[daft.DataFrame.limit] method:
Output โญโโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฎ
โ first_name โ last_name โ age โ DoB โ country โ has_dog โ
โ --- โ --- โ --- โ --- โ --- โ --- โ
โ Utf8 โ Utf8 โ Int64 โ Date โ Utf8 โ Boolean โ
โโโโโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโโโชโโโโโโโโโโโโโชโโโโโโโโโโชโโโโโโโโโโก
โ Wolfgang โ Winter โ 23 โ 2001-02-12 โ Germany โ None โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโค
โ Ernesto โ Evergreen โ 34 โ 1990-04-03 โ Canada โ true โ
โฐโโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโฏ
(Showing first 2 of 2 rows)
To drop columns from the DataFrame, use the df.exclude()
method.
Output 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 โญโโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโฎ
โ first_name โ last_name โ age โ country โ has_dog โ
โ --- โ --- โ --- โ --- โ --- โ
โ Utf8 โ Utf8 โ Int64 โ Utf8 โ Boolean โ
โโโโโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโโโชโโโโโโโโโโโโโโโโโชโโโโโโโโโโก
โ Ernesto โ Evergreen โ 34 โ Canada โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ James โ Jale โ 62 โ Canada โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Shandra โ Shamas โ 57 โ United Kingdom โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Zaya โ Zaphora โ 40 โ United Kingdom โ true โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Wolfgang โ Winter โ 23 โ Germany โ None โ
โฐโโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโโฏ
(Showing first 5 of 5 rows)
Expressions are an API for defining computation that needs to happen over columns. For example, use the daft.col()
expressions together with the with_column
method to create a new column called full_name
, joining the contents from the last_name
column with the first_name
column:
Output 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 โญโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโฎ
โ full_name โ age โ country โ has_dog โ
โ --- โ --- โ --- โ --- โ
โ Utf8 โ Int64 โ Utf8 โ Boolean โ
โโโโโโโโโโโโโโโโโโโโโชโโโโโโโโชโโโโโโโโโโโโโโโโโชโโโโโโโโโโก
โ Wolfgang Winter โ 23 โ Germany โ None โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Shandra Shamas โ 57 โ United Kingdom โ true โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Zaya Zaphora โ 40 โ United Kingdom โ true โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Ernesto Evergreen โ 34 โ Canada โ true โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ James Jale โ 62 โ Canada โ true โ
โฐโโโโโโโโโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโโฏ
(Showing first 5 of 5 rows)
Alternatively, you can also run your column transformation using Expressions directly inside your df.select()
method*:
Output 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 โญโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโฎ
โ full_name โ age โ country โ has_dog โ
โ --- โ --- โ --- โ --- โ
โ Utf8 โ Int64 โ Utf8 โ Boolean โ
โโโโโโโโโโโโโโโโโโโโโชโโโโโโโโชโโโโโโโโโโโโโโโโโชโโโโโโโโโโก
โ Shandra Shamas โ 57 โ United Kingdom โ true โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Zaya Zaphora โ 40 โ United Kingdom โ true โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Wolfgang Winter โ 23 โ Germany โ None โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Ernesto Evergreen โ 34 โ Canada โ true โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ James Jale โ 62 โ Canada โ true โ
โฐโโโโโโโโโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโโฏ
(Showing first 5 of 5 rows)
Sort Data You can sort a DataFrame with the df.sort()
, in this example we chose to sort in ascending order:
Output 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 โญโโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโฎ
โ first_name โ last_name โ age โ DoB โ country โ has_dog โ full_name โ
โ --- โ --- โ --- โ --- โ --- โ --- โ --- โ
โ Utf8 โ Utf8 โ Int64 โ Date โ Utf8 โ Boolean โ Utf8 โ
โโโโโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโโโชโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโชโโโโโโโโโโชโโโโโโโโโโโโโโโโโโโโก
โ Wolfgang โ Winter โ 23 โ 2001-02-12 โ Germany โ None โ Wolfgang Winter โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค
โ Ernesto โ Evergreen โ 34 โ 1990-04-03 โ Canada โ true โ Ernesto Evergreen โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค
โ Zaya โ Zaphora โ 40 โ 1984-04-07 โ United Kingdom โ true โ Zaya Zaphora โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค
โ Shandra โ Shamas โ 57 โ 1967-01-02 โ United Kingdom โ true โ Shandra Shamas โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค
โ James โ Jale โ 62 โ 1962-03-24 โ Canada โ true โ James Jale โ
โฐโโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโฏ
(Showing first 5 of 5 rows)
Group and Aggregate Data You can group and aggregate your data using the df.groupby()
and the df.agg()
methods. A groupby aggregation operation over a dataset happens in 2 steps:
Split the data into groups based on some criteria using df.groupby()
Specify how to aggregate the data for each group using df.agg()
Output 1
2
3
4
5
6
7
8
9
10
11
12
13 โญโโโโโโโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฎ
โ country โ avg_age โ has_dog โ
โ --- โ --- โ --- โ
โ Utf8 โ Float64 โ UInt64 โ
โโโโโโโโโโโโโโโโโโชโโโโโโโโโโชโโโโโโโโโโก
โ United Kingdom โ 48.5 โ 2 โ
โโโโโโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโค
โ Canada โ 48 โ 2 โ
โโโโโโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโค
โ Germany โ 23 โ 0 โ
โฐโโโโโโโโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโฏ
(Showing first 3 of 3 rows)
Note
The df.alias()
method renames the given column.
What's Next? Now that you have a basic sense of Daftโs functionality and features, here are some more resources to help you get the most out of Daft:
Check out our Core Concepts for more details about:
Work with your favorite table and catalog formats :
Coming from?
Try your hand at some Tutorials :