Mars 数据框#

要快速了解Mars DataFrame,请访问 10分钟了解Mars DataFrame

Mars DataFrame 可以像 pandas DataFrame 一样初始化。

>>> import mars.dataframe as md
>>> md.DataFrame({'a': [1, 2, 3], 'b': ['s1', 's2', 's3']})
DataFrame <op=DataFrameDataSource, key=12ee87049f2f1125ffaa84e91f790249>

Pandas DataFrame 也可以传递给 Mars DataFrame。

>>> import pandas as pd
>>> md.DataFrame(pd.DataFrame([[1, 2], [3, 4]]))
DataFrame <op=DataFrameDataSource, key=853b0d99cd26ec82751524899172eb8c>

从Mars张量创建Mars DataFrame。

>>> md.DataFrame(mt.random.rand(3, 3))
DataFrame <op=DataFrameFromTensor, key=10a421ed18adfa42cb649aa575a1d763>

Mars 数据框可以从 CSV 文件、SQL 表等读取数据。

>>> md.read_csv('Downloads/ratings.csv')
DataFrame <op=DataFrameReadCSV, key=48550937383cbea63d4f9f24f3eb1a17>

Mars DataFrame 还支持通过提供 URL 从 HDFS 读取数据。

>>> df = md.read_csv('hdfs://localhost:8020/test.csv')

有关数据框创建的更多信息,请参考 工厂函数

像Mars张量一样,DataFrame也是惰性求值的。 如果你想要获取结果, .execute()需要被调用。

>>> df = md.read_csv('Downloads/ratings.csv')
>>> grouped = df.groupby('movieId').agg({'rating': ['min', 'max', 'mean', 'std']})
>>> grouped.execute()
        rating
           min  max      mean       std
movieId
1          0.5  5.0  3.921240  0.889012
2          0.5  5.0  3.211977  0.951150
3          0.5  5.0  3.151040  1.006642
4          0.5  5.0  2.861393  1.095702
5          0.5  5.0  3.064592  0.982140
...        ...  ...       ...       ...
131254     4.0  4.0  4.000000       NaN
131256     4.0  4.0  4.000000       NaN
131258     2.5  2.5  2.500000       NaN
131260     3.0  3.0  3.000000       NaN
131262     4.0  4.0  4.000000       NaN

[26744 rows x 4 columns]

请记住,DataFrame.execute() 将返回 DataFrame 本身。

有关更多实现的 DataFrame API,请参考 DataFrame API 参考

为了将Mars DataFrame转换为pandas,可以调用.execute().fetch()。另一种方法是.to_pandas()

>>> type(grouped.execute())
mars.dataframe.core.DataFrame

>>> type(grouped.execute().fetch())
pandas.core.frame.DataFrame

>>> type(grouped.to_pandas())
pandas.core.frame.DataFrame

>>> grouped.to_pandas()
        rating
           min  max      mean       std
movieId
1          0.5  5.0  3.921240  0.889012
2          0.5  5.0  3.211977  0.951150
3          0.5  5.0  3.151040  1.006642
4          0.5  5.0  2.861393  1.095702
5          0.5  5.0  3.064592  0.982140
...        ...  ...       ...       ...
131254     4.0  4.0  4.000000       NaN
131256     4.0  4.0  4.000000       NaN
131258     2.5  2.5  2.500000       NaN
131260     3.0  3.0  3.000000       NaN
131262     4.0  4.0  4.000000       NaN

[26744 rows x 4 columns]

注意

用户应该始终考虑使用 .execute() 而不是 .to_pandas(),因为当DataFrame很大时, .execute() 只会获取头部和尾部行以供显示。另一方面, .to_pandas() 将尝试在服务器端生成整个DataFrame并将其返回给客户端,这非常低效且可能导致OutOfMemory错误。

如果需要同时执行多个 DataFrame,可以使用 mars.execute()

>>> import mars
>>> df = md.DataFrame(mt.random.rand(3, 3))
>>> mars.execute(df, df.sum())
(          0         1         2
 0  0.604443  0.743964  0.281236
 1  0.778034  0.634661  0.237829
 2  0.886275  0.456751  0.340311,
 0    2.268752
 1    1.835377
 2    0.859376
 dtype: float64)

DataFrame 可以保存为 CSV 等格式。

>>> df.to_csv('Downloads/grouped.csv').execute()
Empty DataFrame
Columns: []
Index: []

有关更多信息,请参阅 api.dataframe.io

数据框可以从保存在 OSS 中的 CSV 文件读取。

>>> import mars.dataframe as md
>>> from mars.lib.filesystem.oss import build_oss_path
>>> access_key_id = your_oss_id
>>> access_key_secret = your_oss_key
>>> end_point = oss_endpoint
>>> file_path = 'oss://buckey/path/to/data.csv'
>>> auth_path = build_oss_path(file_path, access_key_id, access_key_secret, end_point)
>>> df = md.read_csv(auth_path).execute()

您可以通过 AcessKey Documentation 找到 AccessKey,并通过 Comparison table of OSS Region and Endpoint 获取端点。