python: 다중 열 팬더 데이터 파일 [닫기]

python: 다중 열 팬더 데이터 파일 [닫기]

저는 N .SDF 채우기를 반복하고 glob을 사용하여 목록을 생성하고 각 파일에 대해 일부 계산을 수행한 다음 이 정보를 pandas 데이터 파일 형식으로 저장하는 Python 스크립트를 작성하고 있습니다. 각 파일에 대해 4가지 다른 속성을 계산한다고 가정하면 1000개의 채우기에 대해 예상되는 출력은 5개의 열과 1000개의 행이 있는 데이터 파일 형식으로 요약되어야 합니다. 코드 샘플은 다음과 같습니다.

  # make a list of all .sdf filles present in data folder:
dirlist = [os.path.basename(p) for p in glob.glob('data' + '/*.sdf')]

# create empty data file with 5 columns:
# name of the file,  value of variable p, value of ac, value of don, value of wt
df = pd.DataFrame(columns=["key", "p", "ac", "don", "wt"])

# for each sdf file get its name and calculate 4 different properties: p, ac, don, wt
for sdf in dirlist:
        sdf_name=sdf.rsplit( ".", 1 )[ 0 ]
        # set a name of the file
        key = f'{sdf_name}'
        mol = open(sdf,'rb')
        # --- do some specific calculations --
        p = MolLogP(mol) # coeff conc-perm
        ac = CalcNumLipinskiHBA(mol)#
        don = CalcNumLipinskiHBD(mol)
        wt = MolWt(mol)
        # add one line to DF in the following order : ["key", "p", "ac", "don", "wt"]
        df[key] = [p, ac, don, wt]

문제는 모든 계산을 한 줄로 요약하고 처리된 파일과 함께 DF에 추가해야 하는 스크립트의 마지막 줄에 있습니다. 궁극적으로 1000개의 SDF 채우기를 처리하려면 내 DF에 5개의 열과 1000개의 행이 포함되어야 합니다.

답변1

# make a list of all .sdf filles present in data folder:
dirlist = [os.path.basename(p) for p in glob.glob('data' + '/*.sdf')]

# create empty data file with 5 columns:
# name of the file,  value of variable p, value of ac, value of don, value of wt

# for each sdf file get its name and calculate 4 different properties: p, ac, don, wt

holder = []
for sdf in dirlist:
        sdf_name=sdf.rsplit( ".", 1 )[ 0 ]
        # set a name of the file
        key = f'{sdf_name}'
        mol = open(sdf,'rb')
        # --- do some specific calculations --
        p = MolLogP(mol) # coeff conc-perm
        ac = CalcNumLipinskiHBA(mol)#
        don = CalcNumLipinskiHBD(mol)
        wt = MolWt(mol)
        # add one line to DF in the following order : ["key", "p", "ac", "don", "wt"]
        output_list = pd.Series([key, p, ac, don, wt])
        holder.append(output_list)

df = pd.concat(holder, axis = 1)
df.rename(columns={0:"key", 1:"p", 2:"ac", 3:"don", 4:"wt"], inplace = True)
print(df)

관련 정보