Browse, Search, Retrieve, and File Export Python Pandas Data

Asked 2 years ago, Updated 2 years ago, 257 views

I'm almost a beginner, so I'd appreciate your help.

You want to retrieve data by referring to one file (length.txt, approximately 400,000 lines x 2 columns, left-hand view) and then searching and matching data from another file (effect.txt, approximately 40,000 lines x 2 columns, right-hand view) to a text file.

Specifically, the flow is as follows:

Although I am a beginner, I have written the following code, but it doesn't work.

import pandas as pd

length=pd.read_table('length.txt', delim_whitespace=True)
effect=pd.read_table('effect.txt', delim_whitespace=True)

with open('effect_out.txt', "w") asew:
  for i, jin zip (length['locus'], length['length']):
    effect [ effect [ 'snp' ] == i ]
    effect.head(j).T
    for ext in effect:
      ew.write(ext+'\n')

Enter a description of the image here

python pandas bioinformatics

2022-10-08 01:01

1 Answers

Create length.txt and effect.txt appropriately (separated by TAB).

import pandas as pd

length=pd.read_table('length.txt', delim_whitespace=True)
effect=pd.read_table('effect.txt', delim_whitespace=True)

effect=effect.set_index('snp')
dfx=length.apply(lambdai:effect.loc [i['locus']: i.sum()-1, 'effect'].reset_index(drop=True),axis=1)
dfx.to_csv('effect_out.txt', sep='\t', na_rep='#N/A', index=False, header=None)

length.txt

locus length
1   10
2   10
3   10
4   10

effect.txt

 snp effect
1   1
2   3
3   -5
4   0
5   14
6   -1
7   8
8   6
9   -2
10  0

effect_out.txt

 1.0 3.0 - 5.0 0.0 14.0 - 1.0 8.0 6.0 - 2.0 0.0
3.0-5.00.0 14.0-1.0 8.0 6.0-2.00.0#N/A
-5.00.0 14.0 -1.0 8.0 6.0 -2.00.0 #N/A #N/A
0.014.0-1.0 8.0 6.0-2.000.0#N/A#N/A#N/A


2022-10-08 01:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.