我有一个以行 id 为索引的化学信息学数据集(输入数据(,编码器函数正在将我的微笑字符串转换为 numpy ndarray 形式的二进制数。我想在我的输入数据帧中添加另一列作为指纹,但在转换为熊猫系列时出现错误。谁能告诉我怎么做?
for index, row in input_table.iterrows():
fp_a=(mhfp_encoder.secfp_from_smiles(row['usmiles_c'])) #creates a binary num
column_series = pd.Series(fp_a)
input_table['new_col']=pd.Series(fp_a)
错误:值的长度与索引的长度不匹配
你得到错误,因为 pd。"系列"为您提供了一个包含 2048 行(MHFP 指纹的位长度(的数据帧,但您的数据帧具有其他行数。
您可以通过另一种方式将指纹附加到数据帧。
如果您有这样的数据帧
import pandas as pd
smiles = ['CCC(C)(C)N', 'NCC(O)CO', 'NCCN1CCNCC1','NCCN']
input_table = pd.DataFrame(smiles, columns=['usmiles_c'])
print(input_table)
usmiles_c
0 CCC(C)(C)N
1 NCC(O)CO
2 NCCN1CCNCC1
3 NCCN
并像这样制作指纹
from mhfp.encoder import MHFPEncoder
mhfp_encoder = MHFPEncoder()
fps = []
for smiles in input_table['usmiles_c']:
fp = mhfp_encoder.secfp_from_smiles(smiles)
fps.append(fp)
您可以将整个指品脱附加到一列中
input_table['new_col'] = fps
print(input_table)
usmiles_c new_col
0 CCC(C)(C)N [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0
1 NCC(O)CO [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0
2 NCCN1CCNCC1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0
3 NCCN [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0
或为每个位创建一个单独的列
col_name = range(len(fps[0]))
for n in col_name:
input_table[n] = [m[n] for m in fps]
print(input_table)
usmiles_c 0 1 2 3 4 5 ... 2041 2042 2043 2044 2045 2046 2047
0 CCC(C)(C)N 0 0 0 0 0 0 ... 0 0 0 0 0 0 0
1 NCC(O)CO 0 0 0 0 0 0 ... 0 0 0 0 0 0 0
2 NCCN1CCNCC1 0 0 0 0 0 0 ... 0 0 0 0 0 0 0
3 NCCN 0 0 0 0 0 0 ... 0 0 0 0 0 0 0