@@ -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
86129It is possible to configure runtime (memory and disk settings) and configuration settings when creating a context.
0 commit comments