This is an automated email from the ASF dual-hosted git repository.
thisisnic pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-cookbook.git
The following commit(s) were added to refs/heads/main by this push:
new 76ff1a6 ARROW-13730: Adding a column to an existing Table (#81)
76ff1a6 is described below
commit 76ff1a6b89ca99fc79dbfaf36d26cdd2b91e3164
Author: Alessandro Molina <[email protected]>
AuthorDate: Thu Oct 21 10:45:54 2021 +0200
ARROW-13730: Adding a column to an existing Table (#81)
---
python/source/data.rst | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/python/source/data.rst b/python/source/data.rst
index 8fb339c..f00eed1 100644
--- a/python/source/data.rst
+++ b/python/source/data.rst
@@ -182,6 +182,54 @@ We can combine them into a single table using
:func:`pyarrow.concat_tables`:
cast data from one type to another (if `promote=True`). In such cases the
data
will need to be copied and an extra cost will occur.
+Adding a column to an existing Table
+====================================
+
+If you have a table it is possible to extend its columns using
+:meth:`pyarrow.Table.append_column`
+
+Suppose we have a table with oscar nominations for each actress
+
+.. testcode::
+
+ import pyarrow as pa
+
+ oscar_nominations = pa.table([
+ ["Meryl Streep", "Katharine Hepburn"],
+ [21, 12]
+ ], names=["actor", "nominations"])
+
+ print(oscar_nominations)
+
+.. testoutput::
+
+ pyarrow.Table
+ actor: string
+ nominations: int64
+
+it's possible to append an additional column to track the years the
+nomination was won using :meth:`pyarrow.Table.append_column`
+
+.. testcode::
+
+ oscar_nominations = oscar_nominations.append_column(
+ "wonyears",
+ pa.array([
+ [1980, 1983, 2012],
+ [1934, 1968, 1969, 1982]
+ ])
+ )
+
+ print(oscar_nominations)
+
+.. testoutput::
+
+ pyarrow.Table
+ actor: string
+ nominations: int64
+ wonyears: list<item: int64>
+ child 0, item: int64
+
Searching for values matching a predicate in Arrays
===================================================