Python和R通用的数据格式-feather

Part 1. 安装

  • R

    devtools::install_github("wesm/feather/R")
    
  • Python

    pip install feather-format
    

Part 2. 使用

  • Python中读取、保存

    import feather
    # 读取
    df = pd.read_feather('R_data.feather')
    df
    
     # 保存
    df.to_feather('data.feather')
    
报错

报错原因:将DataFrame存储为.feather文件时,需要DataFrame的index为默认的顺序,因此需要将index更改为默认的顺序。

  import feather
  df = df.reset_index()
  df.to_feather('data.feather')

即可保存成功


python中格式

或者

 import pyarrow.feather as feather
 #feather_db = feather.read_feather('my_db.feather')
 feather.write_feather(feather_db, 'my_db_v1.feather', version=1)
  • R语言读取、保存

    library(feather) 
    df <- read_feather('df_diff_from new and old.feather')
    View(df)
    
R读取结果展示

最新补充:不知道为什么R中的feather无法识别python写出的feather了,原因或许可见
https://arrow.apache.org/docs/python/feather.html
根据https://github.com/ICB-DCM/pyABC/issues/315里面的解决方法,利用arrow包可成功读取!(无论Python的哪个版本写出的feather都可以读取)

 df<-arrow::read_feather(‘data.feather')

保存:

path <- "R_data.feather" 
write_feather(df, path) 

参考
https://blog.csdn.net/tanzuozhev/article/details/51088949
https://zhuanlan.zhihu.com/p/247025752

Part 3. 访问剪切板

  • Python

    import pyperclip 
    pyperclip.copy("hello, world")  # 将文字复制进剪切板
    pyperclip.paste()  # 将文字从剪切板复制出来
    
  • R

     # 将剪切板数据读入
     read.table(pipe("pbpaste"),header = F)
    
    # 将剪切板数据输出
     clip<-pipe("pbcopy","w")
     write.table(path,file=clip)
     close(clip)
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容