python和R读取写入csv文件

python 读取csv文件

1
2
3
4
5
6
7
8
9
10
11
12
13
import dask.dataframe
data = dask.dataframe.read_csv(“random.csv”)

import datatable
datatable.fread()

dd=datatable.fread("/media/ggj/BACKUP21/Chordoma/NEW/S1.3_rmDoublets_matrix.csv")

X=dd.to_pandas()

X.index=X['C0']
X.index=X['C0'].values
X=X.drop(['C0'],axis=1)

python 写出文件

使用df2csv,需要输出基因和细胞名字以防万一,细胞名字在用R读取的时候会丢失。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
def df2csv(df,fname,myformats=[],sep=','):
"""
# function is faster than to_csv
# 7 times faster for numbers if formats are specified,
# 2 times faster for strings.
# Note - be careful. It doesn't add quotes and doesn't check
# for quotes or separators inside elements
# We've seen output time going down from 45 min to 6 min
# on a simple numeric 4-col dataframe with 45 million rows.
"""
if len(df.columns) <= 0:
return
Nd = len(df.columns)
Nd_1 = Nd - 1
formats = myformats[:] # take a copy to modify it
Nf = len(formats)
# make sure we have formats for all columns
if Nf < Nd:
for ii in range(Nf,Nd):
coltype = df[df.columns[ii]].dtype
ff = '%s'
if coltype == np.int64:
ff = '%d'
elif coltype == np.float64:
ff = '%f'
formats.append(ff)
fh=open(fname,'w')
fh.write(','.join(df.columns) + '\n')
for row in df.itertuples(index=False):
ss = ''
for ii in xrange(Nd):
ss += formats[ii] % row[ii]
if ii < Nd_1:
ss += sep
fh.write(ss+'\n')
fh.close()

df2csv(dt,"random.csv")
pd.DataFrame(dt.index).to_csv("gene.csv")
pd.DataFrame(dt.column).to_csv("gene.csv")

R 读取csv

1
2
3
4
5
6
7
8
9
10
library(data.table)
dt<-fread("random.csv")
dt<-data.frame(t(dt)) # 转化为行是基因,列为细胞

dt<-fread("S1.3_rmDoublets_matrix.csv",index ="V1")
dt <- as.data.frame(dt)
dt <- data.frame(dt[,-1],row.names = dt$V1)
dt[1:5,1:5]
dt<-data.frame(t(dt)) # 转化为行是基因,列为细胞
dt[1:5,1:5]

R 写出csv

1
2
3
4
library(data.table)
fwrite(df,"random.csv")

fwrite(dge,paste0(name,"_rmDoublets_matrix.csv"),quote = F,row.names = T)