Pandas中的示例数据集

时间:2015-02-09 19:02:07

标签: python pandas

使用R时,方便加载"练习"数据集使用

data(iris)

data(mtcars)

熊猫有类似的东西吗?我知道我可以使用任何其他方法加载,只是好奇是否有任何内置

3 个答案:

答案 0 :(得分:14)

rpy2模块是为此而建的:

from rpy2.robjects import r, pandas2ri
pandas2ri.activate()

r['iris'].head()

产量

   Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
1           5.1          3.5           1.4          0.2  setosa
2           4.9          3.0           1.4          0.2  setosa
3           4.7          3.2           1.3          0.2  setosa
4           4.6          3.1           1.5          0.2  setosa
5           5.0          3.6           1.4          0.2  setosa

大熊猫0.19你可以使用熊猫'拥有rpy接口:

import pandas.rpy.common as rcom
iris = rcom.load_data('iris')
print(iris.head())

产量

   Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
1           5.1          3.5           1.4          0.2  setosa
2           4.9          3.0           1.4          0.2  setosa
3           4.7          3.2           1.3          0.2  setosa
4           4.6          3.1           1.5          0.2  setosa
5           5.0          3.6           1.4          0.2  setosa

rpy2还提供了一种方式to convert R objects into Python objects

import pandas as pd
import rpy2.robjects as ro
import rpy2.robjects.conversion as conversion
from rpy2.robjects import pandas2ri
pandas2ri.activate()

R = ro.r

df = conversion.ri2py(R['mtcars'])
print(df.head())

产量

    mpg  cyl  disp   hp  drat     wt   qsec  vs  am  gear  carb
0  21.0    6   160  110  3.90  2.620  16.46   0   1     4     4
1  21.0    6   160  110  3.90  2.875  17.02   0   1     4     4
2  22.8    4   108   93  3.85  2.320  18.61   1   1     4     1
3  21.4    6   258  110  3.08  3.215  19.44   1   0     3     1
4  18.7    8   360  175  3.15  3.440  17.02   0   0     3     2

答案 1 :(得分:11)

任何公开可用的.csv文件都可以使用其URL快速加载到pandas中。以下是使用存储在UCI存档中的虹膜数据集的示例。

import pandas as pd

file_name = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

df = pd.read_csv(file_name)

df.head()

此处的输出是您刚刚从给定网址加载的.csv文件标题。

>>> df.head()
   5.1  3.5  1.4  0.2  Iris-setosa
0  4.9  3.0  1.4  0.2  Iris-setosa
1  4.7  3.2  1.3  0.2  Iris-setosa
2  4.6  3.1  1.5  0.2  Iris-setosa
3  5.0  3.6  1.4  0.2  Iris-setosa
4  5.4  3.9  1.7  0.4  Iris-setosa

答案 2 :(得分:9)

内置的pandas测试DataFrame非常方便。

makeMixedDataFrame():

In [22]: import pandas as pd

In [23]: pd.util.testing.makeMixedDataFrame()
Out[23]:
     A    B     C          D
0  0.0  0.0  foo1 2009-01-01
1  1.0  1.0  foo2 2009-01-02
2  2.0  0.0  foo3 2009-01-05
3  3.0  1.0  foo4 2009-01-06
4  4.0  0.0  foo5 2009-01-07

其他测试DataFrame选项:

makeDataFrame():

In [24]: pd.util.testing.makeDataFrame().head()
Out[24]:
                   A         B         C         D
acKoIvMLwE  0.121895 -0.781388  0.416125 -0.105779
jc6UQeOO1K -0.542400  2.210908 -0.536521 -1.316355
GlzjJESv7a  0.921131 -0.927859  0.995377  0.005149
CMhwowHXdW  1.724349  0.604531 -1.453514 -0.289416
ATr2ww0ctj  0.156038  0.597015  0.977537 -1.498532

makeMissingDataframe():

In [27]: pd.util.testing.makeMissingDataframe().head()
Out[27]:
                   A         B         C         D
qyXLpmp1Zg -1.034246  1.050093       NaN       NaN
v7eFDnbQko  0.581576  1.334046 -0.576104 -0.579940
fGiibeTEjx -1.166468 -1.146750 -0.711950 -0.205822
Q8ETSRa6uY  0.461845 -2.112087  0.167380 -0.466719
7XBSChaOyL -1.159962 -1.079996  1.585406 -1.411159

makeTimeDataFrame():

In [28]: pd.util.testing.makeTimeDataFrame().head()
Out[28]:
                   A         B         C         D
2000-01-03 -0.641226  0.912964  0.308781  0.551329
2000-01-04  0.364452 -0.722959  0.322865  0.426233
2000-01-05  1.042171  0.005285  0.156562  0.978620
2000-01-06  0.749606 -0.128987 -0.312927  0.481170
2000-01-07  0.945844 -0.854273  0.935350  1.165401