Skip to content

Commit a1bbc1d

Browse files
melt example
1 parent 14c4f22 commit a1bbc1d

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

python/px-arguments.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.3
23+
version: 3.6.8
2424
plotly:
2525
description: Arguments accepted by plotly express functions
2626
display_as: file_settings
@@ -41,6 +41,38 @@ jupyter:
4141

4242
See also the [introduction to plotly express](./plotly-express).
4343

44+
45+
### Tidy Data
46+
47+
Plotly Express operates on "tidy" or "long" data rather than "wide" data. You may pass data in either as a Pandas `DataFrame` objects or as individual array-like objects which PX will assemble into a data frame internally, such as lists, `numpy` arrays or Pandas `Series` objects.
48+
49+
What follows is a very short example of the difference between wide and tidy/long data, and the excellent [Tidy Data in Python blog post](https://www.jeannicholashould.com/tidy-data-in-python.html) contains much more information about the tidy approach to structuring data.
50+
51+
```python
52+
import pandas as pd
53+
print("This is 'wide' data, unsuitable as-is for Plotly Express:")
54+
wide_df = pd.DataFrame(dict(Month=["Jan", "Feb", "Mar"], London=[1,2,3], Paris=[3,1,2]))
55+
wide_df
56+
```
57+
58+
```python
59+
import pandas as pd
60+
print("This is the same data in 'long' format, ready for Plotly Express:")
61+
wide_df = pd.DataFrame(dict(Month=["Jan", "Feb", "Mar"], London=[1,2,3], Paris=[3,1,2]))
62+
tidy_df = wide_df.melt(id_vars="Month")
63+
```
64+
65+
```python
66+
import plotly.express as px
67+
import pandas as pd
68+
69+
wide_df = pd.DataFrame(dict(Month=["Jan", "Feb", "Mar"], London=[1,2,3], Paris=[3,1,2]))
70+
tidy_df = wide_df.melt(id_vars="Month")
71+
72+
fig = px.bar(tidy_df, x="Month", y="value", color="variable", barmode="group")
73+
fig.show()
74+
```
75+
4476
### pandas DataFrame input data
4577

4678
`px` functions supports natively pandas DataFrame. Arguments can either be passed as dataframe columns, or as column names if the `data_frame` argument is provided.

0 commit comments

Comments
 (0)