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

如何从DataFrame的structure列提取晶格参数a、b、c生成新列

提取晶格参数并合并到原DataFrame

核心思路

通过apply方法遍历structure列的每个对象,提取其中Lattice的a、b、c参数,直接添加到原DataFrame,或者生成新DataFrame后合并。

实现方法

方式1:直接添加新列

假设structure中的对象通过属性访问晶格参数(如x.lattice.a):

import pandas as pd

# 直接为原DataFrame添加a、b、c列
df['a'] = df['structure'].apply(lambda x: x.lattice.a)
df['b'] = df['structure'].apply(lambda x: x.lattice.b)
df['c'] = df['structure'].apply(lambda x: x.lattice.c)

如果对象是字典结构(通过键访问,如x['lattice']['a']),则修改为:

df['a'] = df['structure'].apply(lambda x: x['lattice']['a'])
df['b'] = df['structure'].apply(lambda x: x['lattice']['b'])
df['c'] = df['structure'].apply(lambda x: x['lattice']['c'])

方式2:生成新DataFrame后合并

如果需要先单独生成包含晶格参数的DataFrame,再与原DataFrame合并:

# 提取参数生成新DataFrame
lattice_params = df['structure'].apply(lambda x: pd.Series({
    'a': x.lattice.a,
    'b': x.lattice.b,
    'c': x.lattice.c
}))

# 横向合并到原DataFrame
df = pd.concat([df, lattice_params], axis=1)

字典结构同样替换访问方式即可。

异常处理(可选)

如果structure列存在空值(如None),可以添加判断避免报错:

df['a'] = df['structure'].apply(lambda x: x.lattice.a if x is not None else None)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 11:47:10