Skip to content

Commit db49710

Browse files
committed
Updated method chaining examples
1 parent 26b4ac3 commit db49710

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,29 @@ print(df3.shape)
17481748
df_join.head()
17491749
```
17501750
1751-
4. Get the subset of species that match a criterion, and join on that subset. The "inner" join only includes rows where both tables match on the key column; it's a strategy for filtering the first table by the second table.
1751+
4. Aside: Method chaining formatting options
1752+
1753+
``` python
1754+
# Python allows free line breaks inside parens
1755+
df_join = (surveys
1756+
.merge(species, on="species_id", how="left")
1757+
.set_index("record_id")
1758+
)
1759+
```
1760+
1761+
``` python
1762+
# Use parens as nested break points
1763+
df_join = surveys.merge(species, on="species_id", how="left"
1764+
).set_index("record_id")
1765+
```
1766+
1767+
``` python
1768+
# Explicit line continuation
1769+
df_join = surveys.merge(species, on="species_id", how="left") \
1770+
.set_index("record_id")
1771+
```
1772+
1773+
5. Get the subset of species that match a criterion, and join on that subset. The "inner" join only includes rows where both tables match on the key column; it's a strategy for filtering the first table by the second table.
17521774
17531775
``` python
17541776
# Get the taxa column, masking the rows based on which values match "Bird"
@@ -1761,7 +1783,7 @@ print(df3.shape)
17611783
print(df_inner.head())
17621784
```
17631785
1764-
5. Compare with the results of the left join
1786+
6. Compare with the results of the left join
17651787
17661788
``` python
17671789
df_surveys_left = surveys.merge(birds, on="species_id", how="left").set_index("record_id")

README.org

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,28 @@ print(df3.shape)
16101610
df_join.head()
16111611
#+END_SRC
16121612

1613-
4. Get the subset of species that match a criterion, and join on that subset. The "inner" join only includes rows where both tables match on the key column; it's a strategy for filtering the first table by the second table.
1613+
4. Aside: Method chaining formatting options
1614+
#+BEGIN_SRC python
1615+
# Python allows free line breaks inside parens
1616+
df_join = (surveys
1617+
.merge(species, on="species_id", how="left")
1618+
.set_index("record_id")
1619+
)
1620+
#+END_SRC
1621+
1622+
#+BEGIN_SRC python
1623+
# Use parens as nested break points
1624+
df_join = surveys.merge(species, on="species_id", how="left"
1625+
).set_index("record_id")
1626+
#+END_SRC
1627+
1628+
#+BEGIN_SRC python
1629+
# Explicit line continuation
1630+
df_join = surveys.merge(species, on="species_id", how="left") \
1631+
.set_index("record_id")
1632+
#+END_SRC
1633+
1634+
5. Get the subset of species that match a criterion, and join on that subset. The "inner" join only includes rows where both tables match on the key column; it's a strategy for filtering the first table by the second table.
16141635
#+BEGIN_SRC python
16151636
# Get the taxa column, masking the rows based on which values match "Bird"
16161637
birds = species[species["taxa"] == "Bird"]
@@ -1622,7 +1643,7 @@ print(df3.shape)
16221643
print(df_inner.head())
16231644
#+END_SRC
16241645

1625-
5. Compare with the results of the left join
1646+
6. Compare with the results of the left join
16261647
#+BEGIN_SRC python
16271648
df_surveys_left = surveys.merge(birds, on="species_id", how="left").set_index("record_id")
16281649

0 commit comments

Comments
 (0)