1 day: 读取csv文件,并返回列表,再写入另外一个csv文件
import pandas as pd
import csv
def read_one_line_csv():
df_line = pd.read_csv('1111.csv', usecols = [3], names = None) #第三列,去除标题
df_lines = df_line.values.tolist()
result = []
for line in df_lines:
#print(line)
result.append(line[0])
print(result)
return result
results = read_one_line_csv()
data = open ('data.csv', 'w', encoding = 'gbk')
data.write('x\n')
for m in range(len(results)):
data.write(str(results[m]))
data.write('\n')
data.close()
if __name__ == '__main__' :
read_one_line_csv()
2 day: 读取pdb文件,查找分辨率数值,并写入excel表格中
text = 'RESOLUTION'
with open('2qkr.pdb', 'r') as f:
lines =f.readlines()
for line in lines:
if text in line:
result = line.split()
print(result)
data = open('data.csv','w')
data.write('X\n')
for i in range(len(result)):
results = result[i]
data.write(str(results) + '\n')
data.close()
3.读取文件,并将文档中某一个字符替换成特定的字符
for i in range(1,3596):
wiv_id = 'WIV' + str(i) + '.' + 'mol2'
f1 = open(wiv_id, 'r')
content = f1.read()
f1.close()
tmp = content.replace('NONAME', wiv_id)
with open (wiv_id, 'w') as f2:
f2.write(tmp)
f2.close()
4.重命名文件名,将wiv1 改为 wiv00001
import os
def rename():
path='/home/ylx/script/sdf/' #文件夹路径,为了不出现反斜杠‘\’最好在文件夹后面加上‘/’
filelist = os.listdir(path)
for files in filelist: #遍历文件夹下所有的文件
Olddir = os.path.join(path,files) #原来的工作路径
if os.path.isdir(Olddir): #跳过空文件
continue
filename = os.path.splitext(files)[0] #文件名
filetype = os.path.splitext(files)[1] #文件扩展名
split_name = filename.split('v')
print(8-len(filename))
new_name = 'wiv' + (8-len(filename)) * '0' + split_name[1] + filetype #新的文件路径
Newdir = os.path.join(path, new_name)
os.rename(Olddir,Newdir)
rename()
5.csv文件中部分数据重复的行中的数值取出最小,并删除其他重复的行
import pandas as pd
def unique_value(path):
df = pd.read_csv(path)
list1 = list(df['ID'])
list2 = []
for i in list1:
if i not in list2:
list2.append(i)
return list2
def fliter_score_to_csv():
ID_list = unique_value(path)
df = pd.read_csv(path)
filename0 = path.split('.')
filename1 = filename0[0]
for i in ID_list:
sub_df = df[df['ID']== i]
loc = sub_df['E_score2'].idxmin()
df[loc:loc+1].to_csv(filename1 + '_new.csv', mode='a', index=False, header=False)
if __name__ == "__main__":
path = '58fg_g.csv'
unique_value(path)
fliter_score_to_csv()
6.删除某一列数据中重复值所在的行,并保留有重复值的第一行。
def repeat_smiles_filter(path):
name = path.split('.')[0] + '_new.csv'
df = pd.read_csv(path)
result = df.drop_duplicates(subset=['mol'],keep='first')
return result.to_csv(name, index=None)
if __name__=="__main__":
path1 = 'A2_1.csv'
repeat_smiles_filter(path1)
7.Pandas在读取csv时如何设置列名
(1)csv文件自带列标题
原始数据是有列标的,用excel打开是这样的:
、
import pandas as pd
df_example = pd.read_csv('Pandas_example_read.csv')
df_example = pd.read_csv('Pandas_example_read.csv', header=0) #效果一样
结果都是:
(2)csv文件有列标题,但是想自己换成别的列标题:
2.1 在读数之后自定义标题:
df_example = pd.read_csv('Pandas_example_read.csv')
df_example.columns = ['A','B','C']
df_example = pd.read_csv('Pandas_example_read.csv', names=['A', 'B','C'])
df_example = pd.read_csv('Pandas_example_read.csv', header=0, names=['A', 'B','C']) #效果一样
读取文件,并且改列名:
(3)csv文件没有列标题,从第一行就直接开始是数据的录入了:
原始数据是没有列标的,用excel打开呈现是这样的:
df_example_noCols = pd.read_csv('Pandas_example_read_withoutCols.csv', header=None)
这个时候一定要加'header=None', 这样读进来的列名就是系统默认的0,1,2... 序列号:
(4)csv文件没有列标题,但是自己想加上列标题:
4.1 读进来数之后加上标题
df_example_noCols = pd.read_csv('Pandas_example_read_withoutCols.csv', header=None)
df_example_noCols.columns = ['A', 'B','C']
df_example_noCols = pd.read_csv('Pandas_example_read_withoutCols.csv', header=None, names=['A', 'B','C']) #效果都一样
结果为:
注意:这里不可以用'header=0', 用了之后就会导致第一行的数据先被当成了列名,然后又被重命名覆盖,结果是第一行的数据丢失。本部分转载于:https://www.jianshu.com/p/ab0c62ee002b
8.利用线性回归模型做多特征数据分析
import pandas as pd
from sklearn.linear_model import LogisticRegression
import joblib
input_path = 'test.csv'
out = 'LR_model'
df = pd.read_csv(input_path)
df_feature = df[['E_score2', 'E_strain', 'LogP', 'MW', 'HA', 'HD', 'TPSA'] ]
feature = df_feature.values #let each line of dataframe to array
labels = df.Label
X = feature
y = labels
model = LogisticRegression(random_state=0, solver='lbfgs')
model.fit(X, y)
joblib.dump(model, out+'.pkg')
model_path = 'LR_model.pkg'
model = joblib.load(clf_path)
y_pred = model.predict_proba(X)[:,1]