Python every day!

文章目錄

Apri 19, 2019

1.How to know/change current directory in Python shell?

import os
os.getcwd() # know current directory
os.chdir("/tmp/") # change curren directory

2.How to use python virtual envirment?

sudo apt update
sudo apt install python3-dev python3-pip
sudo pip3 install -U virtualenv
virtualenv --system-site-packages -p python3 ./venv
source ./venv/bin/activate
deactivate

3.How to call external commands in Python script?

from subprocess import call
call(['prepare_ligand4.py', '-l {0} -o {1}/{2}.pdbqt -A checkhydrogens'.format("/tmp/ligand.mol2", library_PostPrepare_folder, mol_id)])

4.How to list all filenames and subdirectories of a directory?

os.listdir(path)

4.1 List only files:

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
import glob
mypath = "/home/zdx/Downloads/topoII/*"
print(glob.glob(mypath))

5.map

ligand_name = list(map(fun, var))

6.dataframe

from pandas import Series,DataFrame
import pandas as pd
data = {'state':ligand_name,'cnnscore':cnnscore}  
df = DataFrame(data)

7.sort dataframe

df =  df.sort_values(by='cnnscore', ascending=False)

8.output dataframe

df.to_csv(f_t, sep='\t', index=False, encoding='utf-8')

9.read excel and csv

df = pd.read_excel('topoII.true_label.xlsx', index_col=0) 
df = pd.read_csv(score_file, header = None, names = ['label', 'score'])

10.locate values

df_t[df_t['label']==0].index.values.astype(int)

11.dataframe batch process

df['ID'] = df['ID'].apply(extract_number)

12.delete column from dataframe

del df['column_name']

13.index dataframe

df.iat[0,0]

14.shuffle dataframe

df = df.sample(frac=1).reset_index(drop=True) # dataframe shuffle
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章