diff --git a/README.md b/README.md index 6e342a9..50e5134 100644 --- a/README.md +++ b/README.md @@ -3184,16 +3184,16 @@ plt.show() # Displays the plot. Also plt.sav ``` ```python ->>> sr = pd.Series([1, 2], index=['x', 'y']) -x 1 -y 2 +>>> sr = pd.Series([2, 3], index=['x', 'y']) +x 2 +y 3 ``` ```text +---------------+-------------+-------------+---------------+ | | 'sum' | ['sum'] | {'s': 'sum'} | +---------------+-------------+-------------+---------------+ -| sr.apply(…) | 3 | sum 3 | s 3 | +| sr.apply(…) | 5 | sum 5 | s 5 | | sr.agg(…) | | | | +---------------+-------------+-------------+---------------+ ``` @@ -3377,11 +3377,12 @@ c 7 8 6 ``` ```python ->>> gb = df.groupby('z') - x y z -3: a 1 2 3 -6: b 4 5 6 - c 7 8 6 +>>> gb = df.groupby('z'); gb.apply(print) + x y z +a 1 2 3 + x y z +b 4 5 6 +c 7 8 6 ``` ```text @@ -3414,9 +3415,9 @@ c 7 8 6 Plotly ------ ```python -# $ pip3 install plotly kaleido -from plotly.express import line -
= line(, x=, y=) # Or: line(x=, y=) +# $ pip3 install pandas plotly kaleido +import pandas as pd, plotly.express as ex +
= ex.line(, x=, y=) # Or: ex.line(x=, y=)
.update_layout(margin=dict(t=0, r=0, b=0, l=0), …) # `paper_bgcolor='rgb(0, 0, 0)'`.
.write_html/json/image('') # Also
.show(). ``` @@ -3424,7 +3425,7 @@ from plotly.express import line #### Displays a line chart of total coronavirus deaths per million grouped by continent: ![Covid Deaths](web/covid_deaths.png) -
+
```python covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv', @@ -3437,7 +3438,7 @@ df = df.groupby(['Continent_Name', 'date']).sum().reset_index() df['Total Deaths per Million'] = df.total_deaths * 1e6 / df.population df = df[df.date > '2020-03-14'] df = df.rename({'date': 'Date', 'Continent_Name': 'Continent'}, axis='columns') -line(df, x='Date', y='Total Deaths per Million', color='Continent').show() +ex.line(df, x='Date', y='Total Deaths per Million', color='Continent').show() ``` #### Displays a multi-axis line chart of total coronavirus cases and changes in prices of Bitcoin, Dow Jones and gold: diff --git a/index.html b/index.html index 2c7e2d3..70d0032 100644 --- a/index.html +++ b/index.html @@ -2599,14 +2599,14 @@ plt.show() # Disp <Sr> = <Sr>.fillna(<el>) # Or: <Sr>.agg/transform/map(lambda <el>: <el>) -
>>> sr = pd.Series([1, 2], index=['x', 'y'])
-x    1
-y    2
+
>>> sr = pd.Series([2, 3], index=['x', 'y'])
+x    2
+y    3
 
┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓
 ┃               │    'sum'    │   ['sum']   │ {'s': 'sum'}  ┃
 ┠───────────────┼─────────────┼─────────────┼───────────────┨
-┃ sr.apply(…)   │      3      │    sum  3   │     s  3      ┃
+┃ sr.apply(…)   │      5      │    sum  5   │     s  5      ┃
 ┃ sr.agg(…)     │             │             │               ┃
 ┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛
 
@@ -2756,12 +2756,12 @@ c  7  8  # Or: <GB>.transform(lambda <Sr>: <Sr>)
 
-
>>> gb = df.groupby('z')
-      x  y  z
-3: a  1  2  3
-6: b  4  5  6
-   c  7  8  6
-
+
>>> gb = df.groupby('z'); gb.apply(print)
+   x  y  z
+a  1  2  3
+   x  y  z
+b  4  5  6
+c  7  8  6
┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓
 ┃                 │    'sum''rank'   │   ['rank']  │ {'x': 'rank'} ┃
 ┠─────────────────┼─────────────┼─────────────┼─────────────┼───────────────┨
@@ -2783,14 +2783,14 @@ c  7  8  
-

#Plotly

# $ pip3 install plotly kaleido
-from plotly.express import line
-<Figure> = line(<DF>, x=<col_name>, y=<col_name>)           # Or: line(x=<list>, y=<list>)
+

#Plotly

# $ pip3 install pandas plotly kaleido
+import pandas as pd, plotly.express as ex
+<Figure> = ex.line(<DF>, x=<col_name>, y=<col_name>)        # Or: ex.line(x=<list>, y=<list>)
 <Figure>.update_layout(margin=dict(t=0, r=0, b=0, l=0), …)  # `paper_bgcolor='rgb(0, 0, 0)'`.
 <Figure>.write_html/json/image('<path>')                    # Also <Figure>.show().
 
-

Displays a line chart of total coronavirus deaths per million grouped by continent:

covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
+

Displays a line chart of total coronavirus deaths per million grouped by continent:

covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
                     usecols=['iso_code', 'date', 'total_deaths', 'population'])
 continents = pd.read_csv('https://gist.githubusercontent.com/stevewithington/20a69c0b6d2ff'
                          '846ea5d35e5fc47f26c/raw/country-and-continent-codes-list-csv.csv',
@@ -2800,7 +2800,7 @@ df = df.groupby(['Continent_Name', 'Total Deaths per Million'] = df.total_deaths * 1e6 / df.population
 df = df[df.date > '2020-03-14']
 df = df.rename({'date': 'Date', 'Continent_Name': 'Continent'}, axis='columns')
-line(df, x='Date', y='Total Deaths per Million', color='Continent').show()
+ex.line(df, x='Date', y='Total Deaths per Million', color='Continent').show()
 
diff --git a/parse.js b/parse.js index ee63083..a326748 100755 --- a/parse.js +++ b/parse.js @@ -294,6 +294,14 @@ const MARIO = 'if __name__ == \'__main__\':\n' + ' main()\n'; +const PLOTLY = + '>>> gb = df.groupby(\'z\'); gb.apply(print)\n' + + ' x y z\n' + + 'a 1 2 3\n' + + ' x y z\n' + + 'b 4 5 6\n' + + 'c 7 8 6'; + const INDEX = '
  • Only available in the PDF.
  • \n' + '
  • Ctrl+F / ⌘F is usually sufficient.
  • \n' + @@ -538,13 +546,13 @@ const DIAGRAM_12_B = '┗━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━┛\n'; const DIAGRAM_13_A = - '| sr.apply(…) | 3 | sum 3 | s 3 |'; + '| sr.apply(…) | 5 | sum 5 | s 5 |'; const DIAGRAM_13_B = "┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + "┃ │ 'sum' │ ['sum'] │ {'s': 'sum'} ┃\n" + "┠───────────────┼─────────────┼─────────────┼───────────────┨\n" + - "┃ sr.apply(…) │ 3 │ sum 3 │ s 3 ┃\n" + + "┃ sr.apply(…) │ 5 │ sum 5 │ s 5 ┃\n" + "┃ sr.agg(…) │ │ │ ┃\n" + "┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n" + "\n" + @@ -816,6 +824,7 @@ function fixHighlights() { $(`code:contains(>>> logging.basicConfig(level=)`).html(LOGGING_EXAMPLE); $(`code:contains(samples_f = (sin(i *)`).html(AUDIO); $(`code:contains(collections, dataclasses, enum, io, itertools)`).html(MARIO); + $(`code:contains(>>> gb = df.groupby)`).html(PLOTLY); $(`ul:contains(Only available in)`).html(INDEX); }