You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3如何提取存储ASCII化学名称的DataFrame中各字段用作标签

迭代提取CSV中化学名称字段的解决方案

你已将CSV读取为Pandas DataFrame对象,要提取ligands列的每个值作为标签,可选择以下几种常用方案:

方案1:直接遍历列值(最常用,仅需取值时推荐)

直接读取ligands列进行遍历即可,代码示例:

import pandas as pd

df = pd.read_csv('Ligand_Ce.list.csv')
for ligand_tag in df['ligands']:
    # 此处直接使用ligand_tag作为标签即可,下方为测试示例
    print(ligand_tag)

方案2:同时获取行索引和字段值

如果需要对应行的序号信息,可以用列的items()方法遍历:

for index, ligand_tag in df['ligands'].items():
    print(f"第{index}个配体标签:{ligand_tag}")

方案3:遍历整行(后续需扩展其他字段时推荐)

如果后续需要读取同一行的其他列数据,可以用iterrows()遍历全表行数据:

for index, row in df.iterrows():
    ligand_tag = row['ligands']
    # 其他列取值方式:row['你的列名']
    print(ligand_tag)

补充说明

如果你的数据存在空值,可先过滤空值再遍历,示例:

for ligand_tag in df['ligands'].dropna():
    print(ligand_tag)

内容的提问来源于stack exchange,提问作者Steve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 18:12:01