0

I have been programming in Java and am trying to learn Python and Graphlab now.

I looked at the Graphlab documentation here but it is insufficient for me to understand how .apply works. There does not seem to be much on Google either.

There are two examples of usage I am trying to figure out:

  1. Where I have a column of country names, and a function to convert "USA" to "United States":

    def transform_country(country)
        if country == 'USA':
            return 'United States'
        else:
            return country
    

    how does the following command / apply work, so that it replaces all instances of "USA" with "United States"? What is .apply doing?

    table['Country'] = sf['Country'].apply(transform_country)
    
  2. In comparison, where I have a column containing a dictionary, say "and" => 5, "stink" => 1, "because = "1", how does .apply work with the function below to display the number associated with "and"?

    Function:

    def awesome_count(word_count):
        if 'and' in word_count:
            return word_count['and']
        else:
            return 0
    

    Command:

    products['awesome'] = products['word_count'].apply(awesome_count)
    
dissipate
  • 13
  • 5
  • Depending on the level of details you want in the answer, it could be a $200M question (http://www.geekwire.com/2016/exclusive-apple-acquires-turi-major-exit-seattle-based-machine-learning-ai-startup/)... So what level of details do you want? – Adrien Renaud Aug 10 '16 at 07:42

1 Answers1

0

Here is a quick demo on use of apply. Its creating a new column in the sframe from an existing sframe column. The new column is squared.

train_data['bedrooms_squared'] = train_data['bedrooms'].apply(lambda x: x**2)
netskink
  • 4,033
  • 2
  • 34
  • 46