Skip to main content

Lightweight representations of networks using Pandas DataFrames.

Project description

networkframe

pypi python Build status Downloads

Lightweight representations of networks using Pandas DataFrames.

networkframe uses Pandas DataFrames to represent networks in a lightweight way. A NetworkFrame object is simply a table representing nodes and a table representing edges, and a variety of methods to make querying and manipulating that data easy.

Warning: networkframe is still in early development, so there may be bugs and missing features. Please report any issues you find!

Examples

Creating a NetworkFrame from scratch:

import pandas as pd

from networkframe import NetworkFrame

nodes = pd.DataFrame(
    {
        "name": ["A", "B", "C", "D", "E"],
        "color": ["red", "blue", "blue", "red", "blue"],
    },
    index=[0, 1, 2, 3, 4],
)
edges = pd.DataFrame(
    {
        "source": [0, 1, 2, 2, 3],
        "target": [1, 2, 3, 1, 0],
        "weight": [1, 2, 3, 4, 5],
    }
)

nf = NetworkFrame(nodes, edges)
print(nf)
NetworkFrame(nodes=(5, 2), edges=(5, 3))

Selecting a subgraph by node color

red_nodes = nf.query_nodes("color == 'red'")
print(red_nodes.nodes)
  name color
0    A   red
3    D   red

Selecting a subgraph by edge weight

strong_nf = nf.query_edges("weight > 2")
print(strong_nf.edges)
   source  target  weight
2       2       3       3
3       2       1       4
4       3       0       5

Iterating over subgraphs by node color

for color, subgraph in nf.groupby_nodes("color", axis="both"):
    print(color)
    print(subgraph.edges)
('blue', 'blue')
   source  target  weight
1       1       2       2
3       2       1       4
('blue', 'red')
   source  target  weight
2       2       3       3
('red', 'blue')
   source  target  weight
0       0       1       1
('red', 'red')
   source  target  weight
4       3       0       5

Applying node information to edges

nf.apply_node_features("color", inplace=True)
print(nf.edges)
   source  target  weight source_color target_color
0       0       1       1          red         blue
1       1       2       2         blue         blue
2       2       3       3         blue          red
3       2       1       4         blue         blue
4       3       0       5          red          red

Is networkframe right for you?

Pros:

  • Lightweight: NetworkFrame objects are just two DataFrames, so they're easy to manipulate and integrate with other tools.
  • Interoperable: can output to NetworkX, numpy and scipy sparse matrices, and other formats (coming soon).
  • Flexible: can represent directed, undirected, and multigraphs.
  • Familiar: if you're familiar with Pandas DataFrames, that is. As much as possible, networkframe uses the same syntax as Pandas, but also just gives you access to the underlying tables.
  • Extensible: it's easy to use NetworkFrame as a base graph - for instance, you could make a SpatialNetworkFrame that adds spatial information to the nodes and edges.

Cons:

  • No guarantees: since networkframe gives you access to the underlying DataFrames, it doesn't do much validation of the data. This is by design, to keep it lightweight and flexible, but it means you can also mess up a NetworkFrame if you aren't careful (for instance, you could delete the index used to map edges to nodes).
  • Not optimized for graph computations: since networkframe is storing data as simple node and edge tables, it's not optimized for doing actual computations on those graphs (e.g. like searching for shortest paths). A typical workflow would be to use networkframe to load and manipulate your data, then convert to a more graph-oriented format like scipy sparse matrices or NetworkX for computations.

Room for improvement:

  • Early development: there are likely bugs and missing features. Please report any issues you find!
  • More interoperability: networkframe can currently output to NetworkX, numpy and scipy sparse matrices, and other formats (coming soon). It would be nice to be able to read in from these formats as well.
  • Graph-type handling: networkframe has mainly been tested on directed graphs, less so for undirected and multigraphs.

Credits

This package was created with Cookiecutter and the bdpedigo/cookiecutter-pypackage project template (which builds on several previous versions).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

networkframe-0.4.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

networkframe-0.4.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file networkframe-0.4.0.tar.gz.

File metadata

  • Download URL: networkframe-0.4.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for networkframe-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b94fbb86b420c55ea4336a264619efa9d6820d77b1a0ccc759c6e4411ea8d40d
MD5 872e9bed86af24f05811672a02ae3969
BLAKE2b-256 5e3b5e6e4b000d7c0142c276a5d0e9e6fdcbcce8414571495b00a0347e5dccf9

See more details on using hashes here.

File details

Details for the file networkframe-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: networkframe-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for networkframe-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf0291c9f23d9a7959dcf451c407ff83f561bad9b7738f71e4d7d835452aa27c
MD5 b3a7e283bc1b0a36e055ba871de27dab
BLAKE2b-256 9743db661c28828587a7e9fb1d35c9528877119ad3d46b72b9ee1a474ad23a68

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page