데이터를 운영하기 위해 두 개의 데이터 프레임을 만들어야하는데 팬더로 할 생각을했습니다.
다음은 제공된 데이터입니다.
class([1,0,0,0],"Small-molecule metabolism ").
class([1,1,0,0],"Degradation ").
class([1,1,1,0],"Carbon compounds ").
function(tb186,[1,1,1,0],'bglS',"beta-glucosidase").
function(tb2202,[1,1,1,0],'cbhK',"carbohydrate kinase").
function(tb727,[1,1,1,0],'fucA',"L-fuculose phosphate aldolase").
function(tb1731,[1,1,1,0],'gabD1',"succinate-semialdehyde dehydrogenase").
function(tb234,[1,1,1,0],'gabD2',"succinate-semialdehyde dehydrogenase").
function(tb501,[1,1,1,0],'galE1',"UDP-glucose 4-epimerase").
function(tb536,[1,1,1,0],'galE2',"UDP-glucose 4-epimerase").
function(tb620,[1,1,1,0],'galK',"galactokinase").
function(tb619,[1,1,1,0],'galT',"galactose-1-phosphate uridylyltransferase C-term").
function(tb618,[1,1,1,0],'galT',"null").
function(tb993,[1,1,1,0],'galU',"UTP-glucose-1-phosphate uridylyltransferase").
function(tb3696,[1,1,1,0],'glpK',"ATP:glycerol 3-phosphotransferase").
function(tb3255,[1,1,1,0],'manA',"mannose-6-phosphate isomerase").
function(tb3441,[1,1,1,0],'mrsA',"phosphoglucomutase or phosphomannomutase").
function(tb118,[1,1,1,0],'oxcA',"oxalyl-CoA decarboxylase").
function(tb3068,[1,1,1,0],'pgmA',"phosphoglucomutase").
function(tb3257,[1,1,1,0],'pmmA',"phosphomannomutase").
function(tb3308,[1,1,1,0],'pmmB',"phosphomannomutase").
function(tb2702,[1,1,1,0],'ppgK',"polyphosphate glucokinase").
function(tb408,[1,1,1,0],'pta',"phosphate acetyltransferase").
function(tb729,[1,1,1,0],'xylB',"xylulose kinase").
function(tb1096,[1,1,1,0],'null',"null").
class([1,1,2,0],"Amino acids and amines ").
function(tb1905,[1,1,2,0],'aao',"D-amino acid oxidase").
function(tb2531,[1,1,2,0],'adi',"ornithine/arginine decarboxylase").
function(tb2780,[1,1,2,0],'ald',"L-alanine dehydrogenase").
function(tb1538,[1,1,2,0],'ansA',"L-asparaginase").
function(tb1001,[1,1,2,0],'arcA',"arginine deiminase").
function(tb753,[1,1,2,0],'mmsA',"methylmalmonate semialdehyde dehydrogenase").
function(tb751,[1,1,2,0],'mmsB',"methylmalmonate semialdehyde oxidoreductase").
그리고 다음과 같은 것을 갖고 싶습니다.
Pandas로 가능합니까? 감사합니다.
네 가능합니다. Bellow가 그 예입니다.
이를 수행하는 방법에는 여러 가지가 있습니다 (일부는 이미 다른 답변에 있음). 이 예제에서는 코드에서 단계를 더 명확하게 만들려고했습니다.
import io
import pandas as pd
with open("file.txt") as f:
lines = f.readlines() # reads your file line by line and returns a list
### sample:
# ['class([1,0,0,0],"Small-molecule metabolism ").\n',
# 'class([1,1,0,0],"Degradation ").\n',
# 'class([1,1,1,0],"Carbon compounds ").\n',
# 'function(tb186,[1,1,1,0],\'bglS\',"beta-glucosidase").\n', ... ]
df1 = []
df2 = []
for line in lines:
# this transformation will be common to all lines
line = line.strip(').\n').replace("[", '"[').replace("]", ']"')
# here we will separate the lines, perform the specific transformation and append them to their specific variable
if line.startswith("class"):
line = line.strip("class(") # specific transform for "class" line
df1.append(line)
else:
line = line.strip("function(") # specific transform for "function" line
df2.append(line)
# in this final block we prepare the variable to be read with pandas and read
df1 = "\n".join(df1) # prepare
df1 = pd.read_csv(
io.StringIO(df1), # as pandas expects a file handler, we use io.StringIO
header=None, # no headers, they are given "manually"
names=['id', 'name'], # headers
)
# the same as before
df2 = "\n".join(df2)
df2 = pd.read_csv(
io.StringIO(df2),
header=None,
names=['orf', 'class', 'genName', 'desc']
)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다