<commit_before>---
layout: global
displayTitle: GraphFrames User Guide
title: User Guide
description: GraphFrames GRAPHFRAMES_VERSION user guide
---

This page gives examples of how to use GraphFrames for basic queries, motif finding, and
general graph algorithms.  This includes code examples in Scala and Python.

* Table of contents (This text will be scraped.)
{:toc}

*Note: Most examples use the GraphFrame from the first subsection:
[Creating GraphFrames](user-guide.html#creating-graphframes).*

# Creating GraphFrames

Users can create GraphFrames from vertex and edge DataFrames.

* *Vertex DataFrame*: A vertex DataFrame should contain a special column named "id" which specifies
  unique IDs for each vertex in the graph.
* *Edge DataFrame*: An edge DataFrame should contain two special columns: "src" (source vertex ID
  of edge) and "dst" (destination vertex ID of edge).

Both DataFrames can have arbitrary other columns.  Those columns can represent vertex and edge
attributes.

A GraphFrame can also be constructed from a single DataFrame containing edge information.
The vertices will be inferred from the sources and destinations of the edges.

<div class="codetabs">

The following example demonstrates how to create a GraphFrame from vertex and edge DataFrames.

<div data-lang="scala"  markdown="1">
{% highlight scala %}
// Vertex DataFrame
val v = sqlContext.createDataFrame(List(
  ("a", "Alice", 34),
  ("b", "Bob", 36),
  ("c", "Charlie", 30),
  ("d", "David", 29),
  ("e", "Esther", 32),
  ("f", "Fanny", 36)
)).toDF("id", "name", "age")
// Edge DataFrame
val e = sqlContext.createDataFrame(List(
  ("a", "b", "friend"),
  ("b", "c", "follow"),
  ("c", "b", "follow"),
  ("f", "c", "follow"),
  ("e", "f", "follow"),
  ("e", "d", "friend"),
  ("d", "a", "friend"),
  ("a", "e", "friend")
)).toDF("src", "dst", "relationship")
// Create a GraphFrame
val g = GraphFrame(v, e)
{% endhighlight %}

The GraphFrame constructed above is available in the GraphFrames package:
{% highlight scala %}
import org.graphframes.examples
val g: GraphFrame = examples.Graphs.friends
{% endhighlight %}
</div>

<div data-lang="python"  markdown="1">
{% highlight python %}
# Vertex DataFrame
v = sqlContext.createDataFrame([
  ("a", "Alice", 34),
  ("b", "Bob", 36),
  ("c", "Charlie", 30),
  ("d", "David", 29),
  ("e", "Esther", 32),
  ("f", "Fanny", 36)
], ["id", "name", "age"])
# Edge DataFrame
e = sqlContext.createDataFrame([
  ("a", "b", "friend"),
  ("b", "c", "follow"),
  ("c", "b", "follow"),
  ("f", "c", "follow"),
  ("e", "f", "follow"),
  ("e", "d", "friend"),
  ("d", "a", "friend")
], ["src", "dst", "relationship"])
# Create a GraphFrame
g = GraphFrame(v, e)
{% endhighlight %}

The GraphFrame constructed above is available in the GraphFrames package:
{% highlight python %}
from graphframes.examples import Graphs
g = Graphs(sqlContext).friends()
{% endhighlight %}
</div>

</div>

# Basic graph and DataFrame queries

GraphFrames provide several simple graph queries, such as node degree.

Also, since GraphFrames represent graphs as pairs of vertex and edge DataFrames, it is easy to make
powerful queries directly on the vertex and edge DataFrames.  Those DataFrames are made available
as `vertices` and `edges` fields in the GraphFrame.

<div class="codetabs">

<div data-lang="scala"  markdown="1">
{% highlight scala %}
import org.graphframes.examples
val g: GraphFrame = examples.Graphs.friends  // get example graph

// Display the vertex and edge DataFrames
g.vertices.show()
// +--+-------+---+
// |id|   name|age|
// +--+-------+---+
// | a|  Alice| 34|
// | b|    Bob| 36|
// | c|Charlie| 30|
// | d|  David| 29|
// | e| Esther| 32|
// | f|  Fanny| 36|
// +--+-------+---+

g.edges.show()
// +---+---+------------+
// |src|dst|relationship|
// +---+---+------------+
// |  a|  b|      friend|
// |  b|  c|      follow|
// |  c|  b|      follow|
// |  f|  c|      follow|
// |  e|  f|      follow|
// |  e|  d|      friend|
// |  d|  a|      friend|
// +---+---+------------+
<commit_msg>forgot Gabby<commit_after>---
layout: global
displayTitle: GraphFrames User Guide
title: User Guide
description: GraphFrames GRAPHFRAMES_VERSION user guide
---

This page gives examples of how to use GraphFrames for basic queries, motif finding, and
general graph algorithms.  This includes code examples in Scala and Python.

* Table of contents (This text will be scraped.)
{:toc}

*Note: Most examples use the GraphFrame from the first subsection:
[Creating GraphFrames](user-guide.html#creating-graphframes).*

# Creating GraphFrames

Users can create GraphFrames from vertex and edge DataFrames.

* *Vertex DataFrame*: A vertex DataFrame should contain a special column named "id" which specifies
  unique IDs for each vertex in the graph.
* *Edge DataFrame*: An edge DataFrame should contain two special columns: "src" (source vertex ID
  of edge) and "dst" (destination vertex ID of edge).

Both DataFrames can have arbitrary other columns.  Those columns can represent vertex and edge
attributes.

A GraphFrame can also be constructed from a single DataFrame containing edge information.
The vertices will be inferred from the sources and destinations of the edges.

<div class="codetabs">

The following example demonstrates how to create a GraphFrame from vertex and edge DataFrames.

<div data-lang="scala"  markdown="1">
{% highlight scala %}
// Vertex DataFrame
val v = sqlContext.createDataFrame(List(
  ("a", "Alice", 34),
  ("b", "Bob", 36),
  ("c", "Charlie", 30),
  ("d", "David", 29),
  ("e", "Esther", 32),
  ("f", "Fanny", 36),
  ("g", "Gabby", 60)
)).toDF("id", "name", "age")
// Edge DataFrame
val e = sqlContext.createDataFrame(List(
  ("a", "b", "friend"),
  ("b", "c", "follow"),
  ("c", "b", "follow"),
  ("f", "c", "follow"),
  ("e", "f", "follow"),
  ("e", "d", "friend"),
  ("d", "a", "friend"),
  ("a", "e", "friend")
)).toDF("src", "dst", "relationship")
// Create a GraphFrame
val g = GraphFrame(v, e)
{% endhighlight %}

The GraphFrame constructed above is available in the GraphFrames package:
{% highlight scala %}
import org.graphframes.examples
val g: GraphFrame = examples.Graphs.friends
{% endhighlight %}
</div>

<div data-lang="python"  markdown="1">
{% highlight python %}
# Vertex DataFrame
v = sqlContext.createDataFrame([
  ("a", "Alice", 34),
  ("b", "Bob", 36),
  ("c", "Charlie", 30),
  ("d", "David", 29),
  ("e", "Esther", 32),
  ("f", "Fanny", 36),
  ("g", "Gabby", 60)
], ["id", "name", "age"])
# Edge DataFrame
e = sqlContext.createDataFrame([
  ("a", "b", "friend"),
  ("b", "c", "follow"),
  ("c", "b", "follow"),
  ("f", "c", "follow"),
  ("e", "f", "follow"),
  ("e", "d", "friend"),
  ("d", "a", "friend"),
  ("a", "e", "friend")
], ["src", "dst", "relationship"])
# Create a GraphFrame
g = GraphFrame(v, e)
{% endhighlight %}

The GraphFrame constructed above is available in the GraphFrames package:
{% highlight python %}
from graphframes.examples import Graphs
g = Graphs(sqlContext).friends()
{% endhighlight %}
</div>

</div>

# Basic graph and DataFrame queries

GraphFrames provide several simple graph queries, such as node degree.

Also, since GraphFrames represent graphs as pairs of vertex and edge DataFrames, it is easy to make
powerful queries directly on the vertex and edge DataFrames.  Those DataFrames are made available
as `vertices` and `edges` fields in the GraphFrame.

<div class="codetabs">

<div data-lang="scala"  markdown="1">
{% highlight scala %}
import org.graphframes.examples
val g: GraphFrame = examples.Graphs.friends  // get example graph

// Display the vertex and edge DataFrames
g.vertices.show()
// +--+-------+---+
// |id|   name|age|
// +--+-------+---+
// | a|  Alice| 34|
// | b|    Bob| 36|
// | c|Charlie| 30|
// | d|  David| 29|
// | e| Esther| 32|
// | f|  Fanny| 36|
// | g|  Gabby| 60|
// +--+-------+---+

g.edges.show()
// +---+---+------------+
// |src|dst|relationship|
// +---+---+------------+
// |  a|  b|      friend|
// |  b|  c|      follow|
// |  c|  b|      follow|
// |  f|  c|      follow|
// |  e|  f|      follow|
// |  e|  d|      friend|
// |  d|  a|      friend|
// |  a|  e|      friend|
// +---+---+------------+

{'fraction_non_alphanumeric': 0.1912394469595008, 'fraction_numerical': 0.010277743790529793, 'mean_word_length': 3.4936778449697634, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7013547', 'n_tokens_mistral': 2862, 'n_tokens_neox': 2631, 'n_words': 958}