Returning compositions/Formulas from pymatgen structures

Hello everybody,

Having a pandas.Series of pymatgen Structure Objects, I was wondering if there’s a specific way to return the related compositions out of Structures, eventually in a separated ‘formula’ column of the dataframe.

Many thanks,

Federico

Hi @Federico_Ottomano , the best way is to retrieve the .composition or composition.reduced_formula attribute of the Strucutre object. If your structures are in a pandas DataFrame, you could write a simple function to do this an then use pandas apply, e.g.

def get_comp(row):
      return row["structure"].composition.reduced_formula

df["new_column"] = df.apply(get_comp, axis=1)

This assumes your DataFrame is called df and the Structure are stored in a column named “structure”. (there may be small mistakes in the above; I suggest cross-checking the syntax in the pandas docs)

Hope this helps.

1 Like

Many thanks! It actually was that simple…^^

Best regards,

Federico