You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by am...@apache.org on 2022/02/24 10:04:09 UTC

[arrow-cookbook] branch main updated: [Python] Add a recipe on how to replace an existing column in Table (#154)

This is an automated email from the ASF dual-hosted git repository.

amolina 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 1ad34d4  [Python] Add a recipe on how to replace an existing column in Table (#154)
1ad34d4 is described below

commit 1ad34d47f81acf83c7aaff009e680d0c08de5ab2
Author: Vibhatha Lakmal Abeykoon <vi...@users.noreply.github.com>
AuthorDate: Thu Feb 24 15:34:05 2022 +0530

    [Python] Add a recipe on how to replace an existing column in Table (#154)
---
 python/source/data.rst | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/python/source/data.rst b/python/source/data.rst
index 970e00c..baeda15 100644
--- a/python/source/data.rst
+++ b/python/source/data.rst
@@ -241,6 +241,59 @@ nomination was won using :meth:`pyarrow.Table.append_column`
     nominations: [[21,12]]
     wonyears: [[[1980,1983,2012],[1934,1968,1969,1982]]]
 
+
+Replacing a column in an existing Table
+=======================================
+
+If you have a table it is possible to replace an existing column using
+:meth:`pyarrow.Table.set_column`
+
+Suppose we have a table with information about items sold at a supermarket
+on a particular day.
+
+.. testcode::
+
+  import pyarrow as pa
+
+  sales_data = pa.table([
+    ["Potato", "Bean", "Cucumber", "Eggs"],
+    [21, 12, 10, 30]
+  ], names=["item", "amount"])
+
+  print(sales_data)
+
+.. testoutput::
+
+    pyarrow.Table
+    item: string
+    amount: int64
+    ----
+    item: [["Potato","Bean","Cucumber","Eggs"]]
+    amount: [[21,12,10,30]]
+
+it's possible to replace the existing column `amount`
+in index `1` to update the sales 
+using :meth:`pyarrow.Table.set_column`
+
+.. testcode::
+
+  new_sales_data = sales_data.set_column(
+    1, 
+    "new_amount",
+    pa.array([30, 20, 15, 40])
+  )
+
+  print(new_sales_data)
+
+.. testoutput::
+
+    pyarrow.Table
+    item: string
+    new_amount: int64
+    ----
+    item: [["Potato","Bean","Cucumber","Eggs"]]
+    new_amount: [[30,20,15,40]]
+
 Searching for values matching a predicate in Arrays
 ===================================================