是否有 Python 的示例数据集?
问题描述
为了快速测试、调试、创建可移植示例和基准测试,R 提供了大量数据集(在 Base R datasets
包中).R 提示符下的命令 library(help="datasets")
描述了近 100 个历史数据集,每个数据集都有相关的描述和元数据.
For quick testing, debugging, creating portable examples, and benchmarking, R has available to it a large number of data sets (in the Base R datasets
package). The command library(help="datasets")
at the R prompt describes nearly 100 historical datasets, each of which have associated descriptions and metadata.
Python 有这样的东西吗?
Is there anything like this for Python?
解决方案
可以使用rpy2
包从 Python 访问所有 R 数据集.
You can use rpy2
package to access all R datasets from Python.
设置界面:
>>> from rpy2.robjects import r, pandas2ri
>>> def data(name):
... return pandas2ri.ri2py(r[name])
然后使用可用数据集的任何数据集名称调用 data()
(就像在 R
中一样)
Then call data()
with any dataset's name of the available datasets (just like in R
)
>>> df = data('iris')
>>> df.describe()
Sepal.Length Sepal.Width Petal.Length Petal.Width
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333
std 0.828066 0.435866 1.765298 0.762238
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000
要查看可用数据集的列表以及每个数据集的描述:
To see a list of the available datasets with a description for each:
>>> print(r.data())
注意:rpy2 需要安装 R
并设置 R_HOME
变量和 pandas
也必须安装.
Note: rpy2 requires R
installation with setting R_HOME
variable, and pandas
must be installed as well.
我刚刚创建了 PyDataset,这是一个简单的模块,可以让从 Python 加载数据集变得如此简单作为 R
的(并且它不需要 R
安装,只需要 pandas
).
I just created PyDataset, which is a simple module to make loading a dataset from Python as easy as R
's (and it does not require R
installation, only pandas
).
要开始使用它,请安装模块:
To start using it, install the module:
$ pip install pydataset
然后只需加载您想要的任何数据集(目前大约有 757 个数据集可用):
Then just load up any dataset you wish (currently around 757 datasets available):
from pydataset import data
titanic = data('titanic')
相关文章