在IPython笔记本中获取r data.frame
使用IPython时,我可以从命令行获取data.frame对象,如下所示:在IPython笔记本中获取r data.frame,python,r,ipython-notebook,rpy2,ipython-magic,Python,R,Ipython Notebook,Rpy2,Ipython Magic,使用IPython时,我可以从命令行获取data.frame对象,如下所示: In [1]: import pandas as pd In [2]: df = pd.DataFrame({'a':[1,2,3],'b':[2,4,6]}) In [3]: df Out[3]: a b 0 1 2 1 2 4 2 3 6 In [4]: %load_ext rmagic In [5]: col =df.columns In [6]: %R -i df,col c
In [1]: import pandas as pd
In [2]: df = pd.DataFrame({'a':[1,2,3],'b':[2,4,6]})
In [3]: df
Out[3]:
a b
0 1 2
1 2 4
2 3 6
In [4]: %load_ext rmagic
In [5]: col =df.columns
In [6]: %R -i df,col colnames(df) <- unlist(col); print(is.matrix(df))
[1] FALSE
In [7]: %R rdf <- data.frame(df); print(is.data.frame(rdf))
[1] TRUE
In [8]: %R print(summary(rdf))
a b
Min. :1.0 Min. :2
1st Qu.:1.5 1st Qu.:3
Median :2.0 Median :4
Mean :2.0 Mean :4
3rd Qu.:2.5 3rd Qu.:5
Max. :3.0 Max. :6
In [9]: %R -d rdf
In [10]: pydf2 = pd.DataFrame(rdf)
[1]中的:将熊猫作为pd导入
[2]中的df=pd.DataFrame({'a':[1,2,3],'b':[2,4,6]})
In[3]:df
出[3]:
a b
0 1 2
1 2 4
2 3 6
在[4]中:%加载
在[5]中:col=df列
在[6]中:%R-i df,col colnames(df)2205返回self.run\u line\u magic(magic\u name,magic\u arg\s)
2206
2207 #-------------------------------------------------------------------------
/run\u line\u magic中的usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc(self,magic\u name,line)
2124 kwargs['local\u ns']=sys.\u getframe(堆栈深度).f\u locals
2125带自建存水弯:
->2126结果=fn(*args,**kwargs)
2127返回结果
2128
/usr/local/lib/python2.7/dist-packages/IPython/extensions/rmagic.pyc in R(self、line、cell、local_ns)
/usr/local/lib/python2.7/dist-packages/IPython/core/magic.pyc in(f,*a,**k)
191#但这一点对国家来说太过分了。
192 def魔术装饰(arg):
-->193调用=λf,*a,**k:f(*a,**k)
194
195如果可调用(arg):
/usr/local/lib/python2.7/dist-packages/IPython/extensions/rmagic.pyc in R(self、line、cell、local_ns)
663如果args.dataframe:
664用于“,”.join(args.dataframe).split(“,”)中的输出:
-->665 self.shell.push({output:self.Rconverter(self.r(output),dataframe=True)})
666
667对于标签,在显示数据中显示:
/Rconverter中的usr/local/lib/python2.7/dist-packages/IPython/extensions/rmagic.pyc(Robj,dataframe)
121 as_data_frame=ro.r('as.data.frame'))
122列=列名称(Robj)
-->123 _name=名称(Robj)
124如果cols!=ri.NULL:
125 Robj=作为数据帧(Robj)
TypeError:“StrVector”对象不可调用
有人知道如何处理这个问题吗
谢谢
软件和系统概要:
- IPython 2.3.0版
- python版本2.7.3
- rpy2版本2.5.2
- R版本3.1.2
- Ubuntu 12.04.5
%load\u ext rpy2.IPython
而不是%load\u ext rmagic
从R获取数据帧。您是否尝试过%load\u ext rpy2.IPython
?%rmagic
最近从ipython
的基地转移到了rpy
的基地。谢谢你告诉我这件事。我现在有一个解决办法%R-d
不起作用,但使用%Rget
我成功地从R获取了data.frame
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-48-04a06535eeef> in <module>()
----> 1 get_ipython().magic(u'R -d rdf')
/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
2203 magic_name, _, magic_arg_s = arg_s.partition(' ')
2204 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2205 return self.run_line_magic(magic_name, magic_arg_s)
2206
2207 #-------------------------------------------------------------------------
/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
2124 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2125 with self.builtin_trap:
-> 2126 result = fn(*args,**kwargs)
2127 return result
2128
/usr/local/lib/python2.7/dist-packages/IPython/extensions/rmagic.pyc in R(self, line, cell, local_ns)
/usr/local/lib/python2.7/dist-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
191 # but it's overkill for just that one bit of state.
192 def magic_deco(arg):
--> 193 call = lambda f, *a, **k: f(*a, **k)
194
195 if callable(arg):
/usr/local/lib/python2.7/dist-packages/IPython/extensions/rmagic.pyc in R(self, line, cell, local_ns)
663 if args.dataframe:
664 for output in ','.join(args.dataframe).split(','):
--> 665 self.shell.push({output:self.Rconverter(self.r(output), dataframe=True)})
666
667 for tag, disp_d in display_data:
/usr/local/lib/python2.7/dist-packages/IPython/extensions/rmagic.pyc in Rconverter(Robj, dataframe)
121 as_data_frame = ro.r('as.data.frame')
122 cols = colnames(Robj)
--> 123 _names = names(Robj)
124 if cols != ri.NULL:
125 Robj = as_data_frame(Robj)
TypeError: 'StrVector' object is not callable