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]

問題は、すべての計算を1行にまとめ、処理されたファイルとともに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)

おすすめ記事