我有一堆 .RData 时间序列文件,想直接将它们加载到 Python 中,而无需先将文件转换为其他扩展名(例如 .csv)。关于实现这一目标的最佳方法的任何想法?
最佳答案
对于那些不想安装 R 来完成这个任务(r2py 需要它)的人来说,有一个新的包“pyreadr”允许将 RData 和 Rds 文件直接读取到 python 中而无需依赖。
它是 C 库 librdata 的包装器,因此速度非常快。
您可以使用 pip 轻松安装:
pip install pyreadr
举个例子:
import pyreadr
result = pyreadr.read_r('/path/to/file.RData') # also works for Rds
# done! let's see what we got
# result is a dictionary where keys are the name of objects and the values python
# objects
print(result.keys()) # let's check what objects we got
df1 = result["df1"] # extract the pandas data frame for object df1
repo 在这里:https://github.com/ofajardo/pyreadr
免责声明:我是这个包的开发者。
关于python - 将 .RData 文件加载到 Python 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21288133/