Wednesday, February 11, 2015

py14. Copy in Python

mtcars is a built-in dataset in R. To load it into Python, you may find a copy at https://raw.githubusercontent.com/ChrisBeaumont/mplfacet/master/mtcars.csv. Download it to your working directory, that is, the directory of the script.


The csv file is loaded using read_csv pandas function, and only first 3 rows and first 2 columns are retained, in cars.


If you extract a column, you are extracting only a view and any modification on the view affects the original object, as well. However if you use copy() on the view, a new object is returned and any changes on it, do not affect original object. While this may seem more complex at first, this leads to more efficient memory management in Python. Most of the time, you are not going to modify the data, so rarely you will need to do a copy() of the view.

# ex14.py
from __future__ import print_function, division
import pandas as pd
# get only 3 row and 2 columns
cars = pd.read_csv("mtcars.csv").iloc[:3,:2]
print("Original cars =\n%s" % cars)
mpg = cars["mpg"]
print("\nOriginal mpg =\n%s" % mpg)
mpg[0] = 42 # Warning
print("\nModified mpg =\n%s" % mpg)
print("\nMazda RX4 mpg modified\n%s" % cars)
cars = pd.read_csv("mtcars.csv").iloc[:3,:2]
print("\nOriginal cars\n%s" % cars)
mpg = cars["mpg"].copy()
print("\nmpg is now a new object")
print("\nmpg before change\n%s" % mpg)
mpg[0] = 42
print("\nmpg after change:\n%s" % mpg)
print("\nMazda RX4 mpg is not modified\n%s" % cars)
#Original cars =
#             #""   mpg
#0      Mazda RX4  21.0
#1  Mazda RX4 Wag  21.0
#2     Datsun 710  22.8
#
#Original mpg =
#0    21.0
#1    21.0
#2    22.8
#Name: mpg, dtype: float64
#
#Modified mpg =
#0    42.0
#1    21.0
#2    22.8
#Name: mpg, dtype: float64
#
#Mazda RX4 mpg modified
#             #""   mpg
#0      Mazda RX4  42.0
#1  Mazda RX4 Wag  21.0
#2     Datsun 710  22.8
#
#Original cars
#             #""   mpg
#0      Mazda RX4  21.0
#1  Mazda RX4 Wag  21.0
#2     Datsun 710  22.8
#
#mpg is now a new object
#
#mpg before change
#0    21.0
#1    21.0
#2    22.8
#Name: mpg, dtype: float64
#
#mpg after change:
#0    42.0
#1    21.0
#2    22.8
#Name: mpg, dtype: float64
#
#Mazda RX4 mpg is not modified
#             #""   mpg
#0      Mazda RX4  21.0
#1  Mazda RX4 Wag  21.0
#2     Datsun 710  22.8

No comments:

Post a Comment