Skip to content

Commit 4b4c641

Browse files
committed
docs: add example for registering a DataFrame as a view in README.md
1 parent 20dbfe8 commit 4b4c641

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,49 @@ This produces the following chart:
8181

8282
![Chart](examples/chart.png)
8383

84+
## Registering a DataFrame as a View
85+
86+
You can use the `into_view` method to convert a DataFrame into a view and register it with the context.
87+
88+
```python
89+
from datafusion import SessionContext, col, literal
90+
91+
# Create a DataFusion context
92+
ctx = SessionContext()
93+
94+
# Create sample data
95+
data = {"a": [1, 2, 3, 4, 5], "b": [10, 20, 30, 40, 50]}
96+
97+
# Create a DataFrame from the dictionary
98+
df = ctx.from_pydict(data, "my_table")
99+
100+
# Filter the DataFrame (for example, keep rows where a > 2)
101+
df_filtered = df.filter(col("a") > literal(2))
102+
103+
# Convert the filtered DataFrame into a view
104+
view = df_filtered.into_view()
105+
106+
# Register the view with the context
107+
ctx.register_table("view1", view)
108+
109+
# Now run a SQL query against the registered view
110+
df_view = ctx.sql("SELECT * FROM view1")
111+
112+
# Collect the results
113+
results = df_view.collect()
114+
115+
# Convert results to a list of dictionaries for display
116+
result_dicts = [batch.to_pydict() for batch in results]
117+
118+
print(result_dicts)
119+
```
120+
121+
This will output:
122+
123+
```python
124+
[{'a': [3, 4, 5], 'b': [30, 40, 50]}]
125+
```
126+
84127
## Configuration
85128

86129
It is possible to configure runtime (memory and disk settings) and configuration settings when creating a context.

0 commit comments

Comments
 (0)