什么是星火数据帧的方法`toPandas`实际上在做什么? [英] What is the Spark DataFrame method `toPandas` actually doing?

查看:236
本文介绍了什么是星火数据帧的方法`toPandas`实际上在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是火花数据框API的初学者。

I'm a beginner of Spark-DataFrame API.

我用这个code加载CSV制表符分隔成星火数据框

I use this code to load csv tab-separated into Spark Dataframe

lines = sc.textFile('tail5.csv')
parts = lines.map(lambda l : l.strip().split('\t'))
fnames = *some name list*
schemaData = StructType([StructField(fname, StringType(), True) for fname in fnames])
ddf = sqlContext.createDataFrame(parts,schemaData)

假设我创建数据框与新文件星火,并使用内置方法toPandas将其转换为大熊猫()

Suppose I create DataFrame with Spark from new files, and convert it to pandas using built-in method toPandas(),


  • 是否熊猫对象存储到本地内存?

  • 熊猫是否低级别的运算处理的所有火花?

  • 它是否公开的所有数据框大熊猫功能?(我猜是)

  • 我可以将其转换toPandas,只是来用它做,没有那么多感人的数据框API?

推荐答案

使用Spark在一个CSV文件读熊猫是很实现的最终目标迂回的方法读一个CSV文件到内存中。

Using spark to read in a CSV file to pandas is quite a roundabout method for achieving the end goal of reading a CSV file into memory.

好像你可能会在这里误解技术的用例的发挥。

It seems like you might be misunderstanding the use cases of the technologies in play here.

火花为分布式计算(虽然它可以在本地使用)。这是一般太重量级用于在CSV文件中简单的阅读。

Spark is for distributed computing (though it can be used locally). It's generally far too heavyweight to be used for simply reading in a CSV file.

在您的例子中, sc.textFile 方法只会给你一个火花RDD,有效地为文本行的列表。这可能不是你想要的。任何类型推断将被执行,所以如果你想在你的CSV文件,总结一列数字,你将不能够因为他们仍然字符串尽可能星火而言。

In your example, the sc.textFile method will simply give you a spark RDD that is effectively a list of text lines. This likely isn't what you want. No type inference will be performed, so if you want to sum a column of numbers in your CSV file, you won't be able to because they are still strings as far as Spark is concerned.

只要使用 pandas.read_csv 和读取整个CSV到内存中。大熊猫将自动推断每列的类型。星火没有做到这一点。

Just use pandas.read_csv and read the whole CSV into memory. Pandas will automatically infer the type of each column. Spark doesn't do this.

现在回答你的问题:

是否存储熊猫对象到本地内存

是的。 toPandas()将星火数据帧转换成熊猫数据帧,这当然是在内存中。

Yes. toPandas() will convert the Spark DataFrame into a Pandas DataFrame, which is of course in memory.

熊猫是否低级别的运算处理的所有火花

没有。熊猫运行其自己的计算,有火花和熊猫之间没有相互作用,有简单的部分的API兼容性。

No. Pandas runs its own computations, there's no interplay between spark and pandas, there's simply some API compatibility.

它是否公开的所有数据框大熊猫功能?

没有。例如,系列对象有一个插值方法,它是不具备的PySpark 的对象。有很多很多方法和函数熊猫API中不属于PySpark的API中。

No. For example, Series objects have an interpolate method which isn't available in PySpark Column objects. There are many many methods and functions that are in the pandas API that are not in the PySpark API.

我可以将其转换toPandas,只是来用它做,没有那么多感人的数据帧API?

当然可以。事实上,你可能甚至不应该在这种情况下使用的Spark在所有。 pandas.read_csv 可能会处理你的使用情况,除非你正在使用一个的巨大的数据量。

Absolutely. In fact, you probably shouldn't even use Spark at all in this case. pandas.read_csv will likely handle your use case unless you're working with a huge amount of data.

尝试用解决你的问题简单,技术含量低,易于理解库和的只有的去的东西,你需要它更加复杂。很多时候,你会不会需要更复杂的技术。

Try to solve your problem with simple, low-tech, easy-to-understand libraries, and only go to something more complicated as you need it. Many times, you won't need the more complex technology.

这篇关于什么是星火数据帧的方法`toPandas`实际上在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆