我对 Python 有点陌生,正在尝试使用 Pandas 模块。下面是我的示例文件(每行的第一个元素是 read_name;第二个元素是 methylation_state;第三个是位置)。
我的目标是首先提取 input_sample1.txt 和 input_sample2.txt 中带有“+”的所有行我能够做到
第二 合并两个数据帧以提取第一个 DF 中而不是第二个 DF 中的位置;然后提取第二个 DF 中而不是第二个 DF 中的位置。
这是我到目前为止所拥有的,并且 m1 和 m2 DF 都出现以下错误:
用户警告: bool 系列键将重新索引以匹配 DataFrame 索引。 “DataFrame 索引。”,用户警告)
#!/usr/bin/env python
from __future__ import print_function
import pandas as pd
import sys
import pandas as pd
df1=pd.read_csv('Input_Sample1.txt', names=['read_name', 'methylation_state', 'position'], usecols=['position', 'methylation_state'], delimiter=r'\s+')
df1 = df1[(df1.methylation_state == '+')]
# print('df1 %s' % ('-' * 50))
# print(df1)
df2=pd.read_csv('Input_Sample2.txt', names=['read_name','methylation_state','position'], usecols=['position', 'methylation_state'], delimiter=r'\s+')
df2 = df2[(df2.methylation_state == '+')]
#print('df2 %s' % ('-' * 50))
#print(df2)
#get an error for the following merged dataframes m1 and m2:
m1=pd.merge(df1, df2, how='left', on='position')
print('df2 - df1 %s' % ('-' * 50))
print(df2[m1['methylation_state_y'].isnull()])
m2 = pd.merge(df1, df2, how='left', on='position')
print('df1 - df2 %s' % ('-' * 50))
print(df1[m2['methylation_state_y'].isnull()])
Input_Sample1.txt:
SRR1035452.114_CRIRUN_726:7:1101:3884:2095_length=36 + 37151024
SRR1035452.114_CRIRUN_726:7:1101:3884:2095_length=36 + 37151031
SRR1035452.114_CRIRUN_726:7:1101:3884:2095_length=36 + 37151189
SRR1035452.117_CRIRUN_726:7:1101:3789:2132_length=36 + 23189251
SRR1035452.117_CRIRUN_726:7:1101:3789:2132_length=36 + 23189248
SRR1035452.117_CRIRUN_726:7:1101:3789:2132_length=36 + 23189242
SRR1035452.117_CRIRUN_726:7:1101:3789:2132_length=36 + 23189086
SRR1035452.117_CRIRUN_726:7:1101:3789:2132_length=36 + 23189101
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644021
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644026
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644032
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644038
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644042
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644050
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644055
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644267
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644253
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644246
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644240
SRR1035452.211_CRIRUN_726:7:1101:5833:2115_length=36 + 60644236
SRR1035452.336_CRIRUN_726:7:1101:8029:2240_length=36 + 26775201
SRR1035452.336_CRIRUN_726:7:1101:8029:2240_length=36 + 26775193
SRR1035452.336_CRIRUN_726:7:1101:8029:2240_length=36 + 26775178
SRR1035452.336_CRIRUN_726:7:1101:8029:2240_length=36 + 26775012
SRR1035452.377_CRIRUN_726:7:1101:9240:2160_length=36 + 27851064
SRR1035452.377_CRIRUN_726:7:1101:9240:2160_length=36 + 27851253
INPUT_SAMPLE2.txt 文件:
SRR1035454.47_CRIRUN_726:7:1101:2618:2094_length=36 - 18921902
SRR1035454.47_CRIRUN_726:7:1101:2618:2094_length=36 + 18921911
SRR1035454.47_CRIRUN_726:7:1101:2618:2094_length=36 + 18921919
SRR1035454.47_CRIRUN_726:7:1101:2618:2094_length=36 + 18921926
SRR1035454.47_CRIRUN_726:7:1101:2618:2094_length=36 + 18922145
SRR1035454.174_CRIRUN_726:7:1101:6245:2159_length=36 + 51460469
SRR1035454.174_CRIRUN_726:7:1101:6245:2159_length=36 + 51460488
SRR1035454.174_CRIRUN_726:7:1101:6245:2159_length=36 + 51460631
SRR1035454.174_CRIRUN_726:7:1101:6245:2159_length=36 + 51460613
SRR1035454.174_CRIRUN_726:7:1101:6245:2159_length=36 + 51460608
SRR1035454.215_CRIRUN_726:7:1101:7106:2100_length=36 - 30309836
SRR1035454.216_CRIRUN_726:7:1101:7129:2116_length=36 + 31856610
SRR1035454.216_CRIRUN_726:7:1101:7129:2116_length=36 + 31856602
SRR1035454.216_CRIRUN_726:7:1101:7129:2116_length=36 + 31856255
SRR1035454.270_CRIRUN_726:7:1101:8134:2171_length=36 + 26078372
SRR1035454.270_CRIRUN_726:7:1101:8134:2171_length=36 + 26078363
SRR1035454.306_CRIRUN_726:7:1101:9223:2098_length=36 + 55329938
SRR1035454.348_CRIRUN_726:7:1101:10157:2107_length=36 + 40179303
SRR1035454.348_CRIRUN_726:7:1101:10157:2107_length=36 + 40179299
SRR1035454.348_CRIRUN_726:7:1101:10157:2107_length=36 + 40179018
部分DF1输入:
0 + 37151024
1 + 37151031
2 + 37151189
3 + 23189251
4 + 23189248
5 + 23189242
6 + 23189086
7 + 23189101
8 + 60644021
9 + 60644026
10 + 60644032
11 + 60644038
12 + 60644042
13 + 60644050
14 + 60644055
15 + 60644267
16 + 60644253
17 + 60644246
18 + 60644240
部分DF2输出:
methylation_state position
1 + 18921911
2 + 18921919
3 + 18921926
4 + 18922145
5 + 51460469
6 + 51460488
7 + 51460631
8 + 51460613
9 + 51460608
11 + 31856610
12 + 31856602
13 + 31856255
14 + 26078372
请注意每个文本文件包含大约 80k 行。非常感谢任何帮助/建议!!
请您参考如下方法:
如何如下
df1 = pd.read_csv('Input_Sample1.txt', names=['read_name', 'methylation_state', 'position'], usecols=['position', 'methylation_state'], delimiter=r'\s+')
df2 = pd.read_csv('Input_Sample2.txt', names=['read_name', 'methylation_state', 'position'], usecols=['position', 'methylation_state'], delimiter=r'\s+')
m1 = df1.merge(df2, how='left') # merge one that exist on df1
m2 = df2.merge(df1, how='left') # merge one that exist on df2
