【问题标题】:Scrpit R with pandas带有熊猫的脚本 R
【发布时间】:2018-10-20 01:57:03
【问题描述】:

我实际上使用 R 来删除重复的序列,例如:

seq1      seq2 
seq2      seq1 
seq3      seq4 
seq5      seq6

得到

seq1      seq2
seq3      seq4 
seq5      seq6

通过做:

data<-read.table("dataframe.txt")

for (i in 1:nrow(data))
{
  data[i,c(2,3)] = sort(data[i,c(2,3)])
}

data2 = data[!duplicated(data[,c(2,3)]),]

write.csv(data2,"data_without_duplicated")

有人知道怎么用熊猫做吗?

这是一个真实数据的例子:

cluster_name    qseqid  sseqid  pident_x    pident_y    length  qstart  qend    sstart  send    qspec   sspec
13  cluster_016663  EOG090X00GO_0035_0035_1 EOG090X00GO_0042_0035_1 0.93    93.0    1179    1   1175    1   1179    0035    0042
14  cluster_016663  EOG090X00GO_0035_0035_1 EOG090X00GO_0042_0042_1 0.93    93.0    1179    1   1175    1   1179    0035    0042
16  cluster_016663  EOG090X00GO_0035_0042_1 EOG090X00GO_0042_0035_1 0.93    93.0    1179    1   1175    1   1179    0035    0042
17  cluster_016663  EOG090X00GO_0035_0042_1 EOG090X00GO_0042_0042_1 0.93    93.0    1179    1   1175    1   1179    0035    0042
19  cluster_016663  EOG090X00GO_0042_0035_1 EOG090X00GO_0035_0035_1 0.93    93.0    1179    1   1179    1   1175    0042    0035
20  cluster_016663  EOG090X00GO_0042_0035_1 EOG090X00GO_0035_0042_1 0.93    93.0    1179    1   1179    1   1175    0042    0035
22  cluster_016663  EOG090X00GO_0042_0042_1 EOG090X00GO_0035_0035_1 0.93    93.0    1179    1   1179    1   1175    0042    0035
23  cluster_016663  EOG090X00GO_0042_0042_1 EOG090X00GO_0035_0042_1 0.93    93.0    1179    1   1179    1   1175    0042    0035

这是我的脚本:

data_wo_eqSpec.to_csv("dataframe.txt", sep='\t')
print("prem1:",data_wo_eqSpec.shape)

data_wo_eqSpec=data_wo_eqSpec.astype(str) 
data_wo_eqSpec.iloc[:,1:3]=np.sort(data_wo_eqSpec.iloc[:,1:3].values,1)

data_wo_eqSpec2= data_wo_eqSpec.drop_duplicates(list(data_wo_eqSpec.iloc[:,1:3]))

print("prem:",data_wo_eqSpec2.shape)

data_wo_eqSpec=pd.read_csv("data_without_duplicated")

print("deus:",data_wo_eqSpec.shape)

这里是数据:data

【问题讨论】:

    标签: r pandas


    【解决方案1】:

    Python

    data.iloc[:,2:4]=np.sort(data.iloc[:,2:4].values,1)
    data2 = data.drop_duplicates(list(data.iloc[:,2:4]))
    

    对于没有 for 循环的 R,使用 apply

    df[!duplicated(t(apply(df[,c(1,2)],1,sort))),]
    

    【讨论】:

    • 您好,事实上,我在尝试使用此代码时似乎遇到了问题:我得到:文件“cluster_test.py”,第 50 行,在 data_wo_eqSpec.iloc[:, 1:3]=np.sort(data_wo_eqSpec.iloc[:,2:4].values,1) 文件“/usr/local/lib/python3.6/site-packages/numpy/core/fromnumeric.py”,第 847 行,在排序 a.sort(axis=axis, kind=kind, order=order) TypeError: '
    • @Benjamin 最好向我们展示数据..或者您只需执行 df=df.astype(str)
    • @Benjamin df=df.astype(str)
    • 我试过了,它运行了,但我没有得到与使用 R 脚本时相同的结果
    • @Benjamin 我认为这只是排序顺序不同
    猜你喜欢
    • 2019-07-29
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多