将R Matrix转换为Pandas Dataframe [英] Convert R Matrix to Pandas Dataframe
本文介绍了将R Matrix转换为Pandas Dataframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将R矩阵转换为熊猫数据框.我正在使用:
I'm trying to convert an R matrix to a pandas dataframe. I am using:
import pandas.rpy.common as com
df = com.convert_to_r_dataframe(r_matrix)
然后我得到:
TypeError: 'float' object cannot be interpreted as an index
奇怪的是,我遇到的所有文档都省略了这种用例.我还将解决将R矩阵转换为numpy
array
的问题-因为无论如何我都希望对行进行迭代.
Strangely enough this use case is omitted from all documentation I have come across. I would also settle for a conversion of the R matrix to a numpy
array
- since I will want to iterate over the rows anyway.
推荐答案
只需使用numpy.array()
:
from rpy2 import robjects
m = robjects.reval("matrix(1:6, nrow=2, ncol=3)")
import numpy as np
a = np.array(m)
这篇关于将R Matrix转换为Pandas Dataframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文