Adding data

Data types

Vizzu currently supports two types of data series: dimensions and measures. Dimensions slice the data cube Vizzu uses, whereas measures are values within the cube.

Dimensions are categorical series that can contain strings and numbers, but both will be treated as strings. Temporal data such as dates or timestamps should also be added as dimensions. By default, Vizzu will draw the elements on the chart in the order they are provided in the data set. Thus we suggest adding temporal data in a sorted format from oldest to newest.

Measures at the moment can only be numerical.

Adding data

There are multiple ways you can add data to ipyvizzu:

using pandas DataFrame

Note: Data().add_data_frame() arguments are:

  • data_frame (mandatory): pandas DataFrame object
  • default_measure_value (optional, default: 0): ipyvizzu fills empty measure values with this number.
  • default_dimension_value (optional, default: ""): ipyvizzu fills the empty dimension values with this string.

Note: ipyvizzu makes a difference between two types of data, numeric (measure) and not numeric (dimension). A column's dtype specifies that the column is handled as a measure or as a dimension.

In [1]:
import pandas as pd

from ipyvizzu import Data

data_frame = pd.DataFrame(
    {
        "Genres": [
            "Pop",
            "Rock",
            "Jazz",
            "Metal",
            "Pop",
            "Rock",
            "Jazz",
            "Metal",
            "Pop",
            "Rock",
            "Jazz",
            "Metal",
        ],
        "Kinds": [
            "Hard",
            "Hard",
            "Hard",
            "Hard",
            "Smooth",
            "Smooth",
            "Smooth",
            "Smooth",
            "Experimental",
            "Experimental",
            "Experimental",
            "Experimental",
        ],
        "Popularity": [114, 96, 78, 52, 56, 36, 174, 121, 127, 83, 94, 58],
    }
)

data_pd = Data()
data_pd.add_data_frame(data_frame)

It is also possible to add pandas.DataFrame.index to Data() with the Data().add_data_frame_index() function. Note: Data().add_data_frame_index() arguments are:

  • data_frame (mandatory): pandas DataFrame object
  • name (mandatory): name of the index series
In [2]:
import pandas as pd

from ipyvizzu import Data

data_frame = pd.DataFrame({"Popularity": [114, 96, 78]}, index=["x", "y", "z"])

data_pd = Data()
data_pd.add_data_frame_index(data_frame, "DataFrameIndex")
data_pd.add_data_frame(data_frame)

When you specify the data by series or by records, it has to be in first normal form. Here is an example of that:

Genres Kinds Popularity
Pop Hard 114
Rock Hard 96
Jazz Hard 78
Metal Hard 52
Pop Smooth 56
Rock Smooth 36
Jazz Smooth 174
Metal Smooth 121
Pop Experimental 127
Rock Experimental 83
Jazz Experimental 94
Metal Experimental 58

Data specified by series

In [3]:
from ipyvizzu import Data

data_series = Data()
data_series.add_series(
    "Genres",
    [
        "Pop",
        "Rock",
        "Jazz",
        "Metal",
        "Pop",
        "Rock",
        "Jazz",
        "Metal",
        "Pop",
        "Rock",
        "Jazz",
        "Metal",
    ],
    type="dimension",
)
data_series.add_series(
    "Kinds",
    [
        "Hard",
        "Hard",
        "Hard",
        "Hard",
        "Smooth",
        "Smooth",
        "Smooth",
        "Smooth",
        "Experimental",
        "Experimental",
        "Experimental",
        "Experimental",
    ],
    type="dimension",
)
data_series.add_series(
    "Popularity", [114, 96, 78, 52, 56, 36, 174, 121, 127, 83, 94, 58], type="measure"
)

Data specified by records

In [4]:
from ipyvizzu import Data

data_records = Data()

data_records.add_series("Genres", type="dimension")
data_records.add_series("Kinds", type="dimension")
data_records.add_series("Popularity", type="measure")

record = ["Pop", "Hard", 114]

data_records.add_record(record)

records = [
    ["Rock", "Hard", 96],
    ["Jazz", "Hard", 78],
    ["Metal", "Hard", 52],
    ["Pop", "Smooth", 56],
    ["Rock", "Smooth", 36],
    ["Jazz", "Smooth", 174],
    ["Metal", "Smooth", 121],
    ["Pop", "Experimental", 127],
    ["Rock", "Experimental", 83],
    ["Jazz", "Experimental", 94],
    ["Metal", "Experimental", 58],
]

data_records.add_records(records)

Data cube

Genres
PopRockJazzMetal
Kinds Hard 114967852
Smooth 563674121
Experimental 127839458
Popularity
In [5]:
from ipyvizzu import Data

data_cube = Data()

data_cube.add_dimension("Genres", ["Pop", "Rock", "Jazz", "Metal"])
data_cube.add_dimension("Kinds", ["Hard", "Smooth", "Experimental"])

data_cube.add_measure(
    "Popularity",
    [
        [114, 96, 78, 52],
        [56, 36, 174, 121],
        [127, 83, 94, 58],
    ],
)

JSON

Content of ./music_data.json (in this example the data stored in the Data Cube format):

{
    "dimensions": [
        {"name": "Genres", "values": [ "Pop", "Rock", "Jazz", "Metal"]},
        {"name": "Kinds", "values": [ "Hard", "Smooth", "Experimental"]}
    ],
    "measures": [
        {
            "name": "Popularity",
            "values":  [
                [114, 96, 78, 52],
                [56, 36, 174, 121],
                [127, 83, 94, 58]
            ]
        }
    ]
}
In [6]:
from ipyvizzu import Data

data_json = Data.from_json("./music_data.json")

Next chapter: Axes, title, tooltip ----- Previous chapter: Intro ----- Back to the Table of contents