繁体   English   中英

python:在 FOR 循环中获得的多列 pandas 数据文件

[英]python: multi-column pandas data-file obtained in FOR loop

我正在研究一个 Python 脚本,该脚本循环 N.SDF 填充,使用 glob 创建它们的列表,为每个文件执行一些计算,然后以 pandas 数据文件格式存储此信息。 假设我计算每个文件的 4 个不同属性,对于 1000 个填充,预期的 output 应该以 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]

问题出在脚本的最后一行,需要将所有计算汇总在一行中,并将 append 与处理后的文件一起放入 DF。 最终,对于 1000 个已处理的 SDF 填充,我的 DF 应该包含 5 列和 1000 行。

你应该用类似的东西替换麻烦的线

df.loc[len(df)] = [key, p, ac, don, wt]

这将 append df末尾的新行

或者你可以做

df = df.append(adict,ignore_index = True)

其中adict是与作为键的列名关联的值的字典:

adict = {'key':key, 'p':p, ...}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM