If you want to extend the frame iteratively then concat
should actually work. This
df1 = pd.DataFrame(columns = ["Al", "Si", "K", "Th"], data = [[1,2,3,4]])
df2 = pd.DataFrame(columns = ["W", "Cu"], data = [[5,6]])
df = pd.concat([df1, df2], axis='rows')
df.fillna(0, inplace=True)
gives you
Al Si K Th W Cu
0 1.0 2.0 3.0 4.0 0.0 0.0
0 0.0 0.0 0.0 0.0 5.0 6.0
Just a suggestion: Wouldn’t you be better off if you do the creation of the underlying data with basic Python only?
Something like
import re
import pandas as pd
re_comps = re.compile(r'([A-Z][a-z]?)([0-9]*)')
formulas = ("NaAlSiO2", "WCu2")
elements = {element for formula in formulas
for element, _ in re_comps.findall(formula)}
perc_dict = {key: len(formulas) * [None] for key in elements.union({'Formula'})}
for i, formula in enumerate(formulas):
perc_dict['Formula'][i] = formula
total_weight = molecular_w_calc(formula)
for element, count in re_comps.findall(formula):
count = 1 if count == '' else int(count)
perc_dict[element][i] = (Element_mass[element] * 100 * count) / total_weight
and only then Pandas
perc_df = pd.DataFrame(perc_dict)
perc_df.set_index('Formula', drop=True, inplace=True)
perc_df.sort_index(axis='columns', inplace=True)
The structure of the resulting perc_df
looks like (the values are obviously wrong, since I didn’t have the Element_mass
dictionary and molecular_w_calc
function):
Al Cu Na O Si W
Formula
NaAlSiO2 1.0 NaN 1.0 2.0 1.0 NaN
WCu2 NaN 2.0 NaN NaN NaN 1.0
CLICK HERE to find out more related problems solutions.