RMagic,IPython和摘要信息

时间:2013-01-16 10:46:56

标签: pandas ipython rpy2 ipython-notebook

按照此处的示例

http://www.randalolson.com/2013/01/14/filling-in-pythons-gaps-in-statistics-packages-with-rmagic/

我在IPython笔记本中的不同数据集上尝试了相同的操作。

https://github.com/burakbayramli/kod/blob/master/delltest/dell.tgz

from pandas import *
orders = read_csv("dell.csv",sep=",")
%load_ext rmagic
%R -i orders print(summary(orders))

我得到了

     Length Class  Mode
[1,] 25     -none- list
[2,] 25     -none- list
[3,] 25     -none- list
..

然而在R

中也是如此
data <- read.csv ("dell.csv",header=TRUE,sep=",")
print (summary(data))

给我正确的摘要信息。

      rank        per_customer_count total_total_amount    orderid     
 Min.   : 1.000   Min.   : 1.000     Min.   :    0.14   Min.   :    1  
 1st Qu.: 2.000   1st Qu.: 6.000     1st Qu.:  866.11   1st Qu.: 2964  
 Median : 4.000   Median : 8.000     Median : 1764.08   Median : 5980  
 Mean   : 4.997   Mean   : 9.426     Mean   : 2004.95   Mean   : 5987  
 3rd Qu.: 7.000   3rd Qu.:12.000     3rd Qu.: 2856.06   3rd Qu.: 9004  
 ...

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我快速看了一下,似乎有许多情况下ipython魔术 没有得到正确的转换。关于rmagic和我,我必须与他们取得联系 更神奇。

与此同时,您应该能够从下面的代码片段中了解您需要的内容:

import pandas
orders = pandas.read_csv("dell.csv", sep=",")
%load_ext rmagic

import rpy2.robjects
d = dict()
for i, (k,v) in enumerate(orders.iteritems()):
    print("%s (type: %s - %i/%i)" %(k, v.dtype.kind, i, orders.shape[1]))
    if v.dtype.kind == 'O':
      v = rpy2.robjects.vectors.StrVector(v)
    d[k] = rpy2.robjects.conversion.py2ri(v)
df = rpy2.robjects.DataFrame(d)

def print_rsummary(x):
    print(rpy2.robjects.baseenv['summary'](x))

print_rsummary(df)