[英]Fastest way to filter csv using pandas and create a matrix
input dict输入字典
{'basename_AM1.csv': ['AM1286', 'AM1287', 'AM1288']}
I have large csv files in the below format basename_AM1.csv I have large csv files in the below format我有以下格式的大型 csv 文件 basename_AM1.csv 我有以下格式的大型 csv 文件
basename_AM1.csv basename_AM1.csv
ID1 ID2 Score
0 AM1287 AM1286 97.55
1 AM1288 AM1286 78.91
2 AM1289 AM1286 95.38
3 AM1290 AM1286 94.83
4 AM1291 AM1286 82.91
Now I need to create a similarity dict like below for the given input_dict by searching/filter the csv files现在我需要通过搜索/过滤 csv 文件为给定的 input_dict 创建一个如下所示的相似性字典
{'AM1286': {'AM1286': 0, 'AM287': 97.55, 'AM288': 78.91},
'AM1287': {'AM1286': 97.55, 'AM1287': 100.0, 'AM1288': 78.91},
'AM1288': {'AM1286': 78.91, 'AM1287': 78.91, 'AM1288': 100.0}}
I have come up with the below logic but for an input_dict of 100 samples this takes too long, Can someone please suggest the optimized and fastest way to achieve this我提出了以下逻辑,但是对于 100 个样本的 input_dict,这需要很长时间,有人可以建议优化和最快的方法来实现这一点
for key,value in input_dict.items():
base_name_df = pd.read_csv('csv_file_path')
base_name_df.columns = "ID1","ID2","Score"
if os.path.exists('csv_file_path'):
for id1 in range(len(value)):
for id2 in range(len(value)):
scan_df = base_name_df[(base_name_df['ID1'] == value[id1]) & (base_name_df['ID2'] == value[id2])]
if not scan_df.empty:
scan_df = scan_df.groupby(['LIMSID1','LIMSID2'], as_index=False)['Score'].max()
final_dict[value[id1]][value[id2]] = scan_df.iloc[0]['Score']
IIUC, you can use: IIUC,您可以使用:
input_dict = {'basename_AM1.csv': ['AM1286', 'AM1287', 'AM1288']}
import pandas as pd
for fname, lst in input_dict.items():
df = pd.read_csv(fname, sep='\s+', names=['ID1', 'ID2', 'score'])
df2 = df.pivot('ID1', 'ID2', 'score').reindex(index=lst, columns=lst)
df2 = df2.combine_first(df2.T).fillna(0)
# print for example
print(df2.to_dict())
If you want 100 on the diagonal:如果你想要 100 在对角线上:
import numpy as np
a = df2.to_numpy()
np.fill_diagonal(a, 100)
df2 = pd.DataFrame(a, index=lst, columns=lst)
output:输出:
{'AM1286': {'AM1286': 0.0, 'AM1287': 97.55, 'AM1288': 78.91},
'AM1287': {'AM1286': 97.55, 'AM1287': 0.0, 'AM1288': 0.0},
'AM1288': {'AM1286': 78.91, 'AM1287': 0.0, 'AM1288': 0.0}}
Pandas has a built in read_csv method. Pandas 有一个内置的 read_csv 方法。
Docs available at: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html文档位于: https ://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.