简体   繁体   English

.apply在Graphlab中如何工作?

[英]How does .apply in Graphlab work?

I have been programming in Java and am trying to learn Python and Graphlab now. 我一直在用Java编程,现在正在尝试学习Python和Graphlab。

I looked at the Graphlab documentation here but it is insufficient for me to understand how .apply works. 我在这里查看了Graphlab文档,但不足以了解.apply的工作方式。 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? .apply在做什么?

     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"? 相比之下,在我有一列包含字典的列的情况下,说“和” => 5,“臭味” => 1,“由于= 1”,。apply如何与下面的函数一起显示与“和”?

    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) 

Here is a quick demo on use of apply. 这是有关应用用法的快速演示。 Its creating a new column in the sframe from an existing sframe column. 它从现有sframe列在sframe中创建一个新列。 The new column is squared. 新列是平方的。

train_data['bedrooms_squared'] = train_data['bedrooms'].apply(lambda x: x**2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM