Skip to content

Where, update and set

Max Gordon edited this page Sep 1, 2016 · 2 revisions

Load the packages

require 'torch'
require 'Dataframe'

Load the data

my_data = Dataframe('realistic_29_row_data.csv')

Checkout the first couple of rows

The simplest example way to have a quick look at the data is to use the output together with head/tail - the simplest form of subsetting

my_data:head(2):output()
my_data:tail(2):output()
# Filename Gender Weight Comments
1 /home/test/wow.png Male 55.5 nan
2 /home/test/wow2.png Female 77 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ex
# Filename Gender Weight Comments
1 /home/test/wow3.png Male 99 nan
2 /home/test/wow4.png Male 99 nan

Searching the dataframe

The where can be convenient when you want to find a particular subset

my_data:where('Gender', 'Male'):head(2):output()
# Filename Gender Weight Comments
1 /home/test/wow.png Male 55.5 nan
2 /home/test/wow5.png Male 78 nan

More flexible searching is allowed through custom search functions that take a row as argument and return boolean

my_data:where(function(row) return row.Gender == "Male" and row.Weight > 70 end):output()
# Filename Gender Weight Comments
1 /home/test/wow5.png Male 78 nan
2 /home/test/wow4.png Male 89 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ex
3 /home/test/wow3.png Male 87
4 /home/test/wow4.png Male 76
5 /home/test/wow5.png Male 88
6 /home/test/wow2.png Male 99
7 /home/test/wow2.png Male 88
8 /home/test/wow3.png Male 99
9 /home/test/wow4.png Male 99

Update

We can easily update the table using an update function

my_data:
    update(
        function(row) return row.Weight > 88 end,
        function(row)
            row.Weight = 88
            return row
        end)

my_data:
    where(function(row) return row.Gender == "Male" and row.Weight > 70 end):
    output()
# Filename Gender Weight Comments
1 /home/test/wow5.png Male 78 nan
2 /home/test/wow4.png Male 88 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ex
3 /home/test/wow3.png Male 87
4 /home/test/wow4.png Male 76
5 /home/test/wow5.png Male 88
6 /home/test/wow2.png Male 88
7 /home/test/wow2.png Male 88
8 /home/test/wow3.png Male 88
9 /home/test/wow4.png Male 88

The set function

Closely related to the update is the simpler set function

my_data:
    set{item_to_find = 55.5, 
        column_name = 'Weight', 
        new_value = Df_Dict({Gender = "Female"})}

my_data:
    where(function(row) return row.Gender == "Female" and row.Weight < 60 end):
    output()
# Filename Gender Weight Comments
1 /home/test/wow.png Female 55.5 nan
2 /home/test/wow5.png Female 57 nan
3 /home/test/wow4.png Female 44 nan
4 /home/test/wow5.png Female 56 nan