(数据科学学习手札05)Python与R数据读入存出方式的总结与比较

在数据分析的过程中,外部数据的导入和数据的导出是非常关键的部分,而Python和R在这方面大同小异,且针对不同的包或模块,对应着不同的函数来完成这部分功能:

Python

1.TXT文件

导入:

以某证券软件导出的txt格式股票数据为例:

 

方式1:

with open(r'C:\Users\windows\Desktop\test\input\SH#600216.txt','r') as s:
    data_raw = s.readlines()
data_raw

可以看到,通过readlines(),目标文件中的每一行都被保存为列表中的一个元素

方式2:

with open(r'C:\Users\windows\Desktop\test\input\SH#600216.txt','r') as s:
    t = []
    while True:
        line = s.readline()
        if line:
            t.append(line)
        else:
            break
t

方式3:

with open(r'C:\Users\windows\Desktop\test\input\SH#600216.txt','r') as s:
    t = s.read()
print(t)

read()读入的是整个txt文件的数据,无视分行:

为了得到每行独立的列表,只需使用spilt()即可:

t.spilt('\n')

写出:

with open(r'C:\Users\windows\Desktop\test\input\SH#600216.txt','r') as s:
    t = s.read()

with open(r'C:\Users\windows\Desktop\test\input\write.txt','w') as w:
    w.write(t)

这是观察文件所在目录,多出了我们生成的txt文件:

 

2.csv文件

读入:

这里我们需要用到pandas包来进行相关操作:

import pandas as pd

data = pd.read_csv(r'C:\Users\windows\Desktop\test\input\重庆美团商品基本信息.csv',engine='python')

 查看data,证实成功读入:

写出:

上面我们完成了对之指定csv文件的读入,并以数据框的形式存放在data中,下面我们将data中的数据写出到新命名的文件中:

data.to_csv(r'C:\Users\windows\Desktop\test\input\new.csv',encoding='ANSI')

这时查看对应目录下的确出现了new.csv:

 

3.xlsx文件

读入:

import pandas as pd

data = pd.read_excel(r'C:\Users\windows\Desktop\test\input\重庆美团商家信息.xlsx')
data.head()

写出:

data.to_excel(r'C:\Users\windows\Desktop\test\input\new.xlsx',encoding='ANSI')

 

R

1.txt文件

读入:

> setwd('C:\\Users\\windows\\Desktop\\test\\output')
> data <- read.table('SH#600216.txt',skip=1,header=T)
> summary(data)
日期 开盘 最高 最低 收盘
2013/03/04: 1 Min. : 8.18 Min. : 8.76 Min. : 8.18 Min. : 8.34
2013/03/05: 1 1st Qu.:10.41 1st Qu.:10.54 1st Qu.:10.28 1st Qu.:10.41
2013/03/06: 1 Median :11.60 Median :11.86 Median :11.39 Median :11.61
2013/03/07: 1 Mean :12.88 Mean :13.10 Mean :12.68 Mean :12.90
2013/03/08: 1 3rd Qu.:13.99 3rd Qu.:14.18 3rd Qu.:13.77 3rd Qu.:14.03
2013/03/11: 1 Max. :24.39 Max. :24.59 Max. :23.80 Max. :24.04
(Other) :1209
成交量 成交额
Min. : 2343203 Min. :2.651e+07
1st Qu.: 7052300 1st Qu.:8.825e+07
Median :13560938 Median :1.664e+08
Mean :17675921 Mean :2.262e+08
3rd Qu.:24076418 3rd Qu.:3.010e+08
Max. :97149939 Max. :1.283e+09

写出:

 

> write.table(data,'new.txt')

 

 

 2.csv文件

读入:

> setwd('C:\\Users\\windows\\Desktop\\test\\output')
> data <- read.csv('重庆美团商品基本信息.csv',header=T,sep=',')
> summary(data)
        月份         平台                      店铺名称     
 2017/5/1 :43841   美团:275343   巫山纸包鱼        :   264  
 2017/4/1 :43154                 黔江鸡杂          :   246  
 2017/6/1 :39231                 街吧              :   203  
 2017/3/1 :38855                 四季颂蛋糕        :   189  
 2016/11/1:37496                 万州烤鱼          :   187  
 2016/12/1:36682                 苒然烘焙(金科店):   180  
 (Other)  :36084                 (Other)           :274074  
                   商品名称            菜系            价格           原价        
 100元代金券1张,可叠加: 16913   甜点饮品:65760   Min.   :   0   Min.   :    0.0  
 10元代金券1张,可叠加 :  4879   火锅    :59417   1st Qu.:  38   1st Qu.:   50.0  
 4人餐,提供免费WiFi   :  4263   川菜    :49437   Median :  90   Median :  130.0  
 20元代金券1张,可叠加 :  3223   小吃快餐:41659   Mean   : 140   Mean   :  207.7  
 8人餐,提供免费WiFi   :  3166   其他美食:12706   3rd Qu.: 168   3rd Qu.:  247.0  
 6人餐,提供免费WiFi   :  3078   烧烤烤肉:11732   Max.   :6888   Max.   :14500.0  
 (Other)               :239821   (Other) :34632   NA's   :38     NA's   :39       
   城市       
 重庆:275343 

写出:

> write.csv(data,file='new.csv')

3.xlsx文件

读入:

方式1:

> library(readxl)
> setwd('C:\\Users\\windows\\Desktop\\test\\output')
> data <- read_excel('重庆美团商家信息.xlsx')
> head(data)
# A tibble: 6 x 15
  数据所属期 平台类型  平台             商家名称     商家电话 商家评分
      <dttm>    <chr> <chr>                <chr>        <chr>    <dbl>
1 2017-06-01     团购  美团 大通冰室(商社汇店)  13983861054      5.0
2 2017-06-01     团购  美团 四季花苑渝湘精致菜馆 023-49815818      4.6
3 2017-06-01     团购  美团   南丫甜(久长街店)  13808360338      5.0
4 2017-06-01     团购  美团 午后蛋糕店(涪陵店)  18225142460      3.9
5 2017-06-01     团购  美团         侯哥风味酒楼  15923287859      3.5
6 2017-06-01     团购  美团             茶颜茶语  13389663358       NA
# ... with 9 more variables: 商家地址 <chr>, 商家评论数 <dbl>, 城市 <chr>, 省 <chr>,
#   本月销量 <dbl>, 本月销售额 <dbl>, 特色菜 <chr>, 菜系 <chr>, 商家URL <chr>

方式2(速度超慢,非常不建议使用!!!):

options(java.parameters = "-Xmx4096m")
library(xlsx)
setwd('C:\\Users\\windows\\Desktop\\test\\output')
data <- read.xlsx('重庆美团商家信息.xlsx',sheetIndex = 1)
head(data)

写出:

目前R的针对excel文件写出的方法中,比较方便(前提是你的电脑安装了java并成功配置好环境)的是xlsx包中的write.xlsx(),如下:

write.xlsx(data,file='demo.xlsx')

Python与R对基本数据类型的读入写出大致如上,而对数据库文件等较复杂数据的处理以后会提及。

posted @ 2018-03-11 21:05  费弗里  阅读(815)  评论(0编辑  收藏  举报