I have a imported a .json list with my data, turning it into a list of dictionaries.
The keys are column headers. For a given value of a certain key I would like to get to the value of another key in the same dictionary.
data = [
{
"Nr.": 2,
"Table data": "S - Sulfur",
"Ref.": 571,
"Formula": "S",
"Name": "Sulfur",
},
{
"Nr.": 3,
"Table data": "HF - Hydrogen Fluoride",
"Ref.": 556,
"Formula": "HF",
"Name": "Hydrogen Fluoride",
},
{
"Nr.": 4,
"Table data": "N2 - Nitrogen",
"Ref.": 5,
"Formula": "N2",
"Name": "Nitrogen",
},
]
so for example given a user input of "Nitrogen" I would like to retrieve the value of the key "Formula" i.e. "N2.
I would convert the list of dictionaries into a dataframe and then use the lookup method.
import pandas
data = pandas.read_json("file.json")
df = pandas.DataFrame(data,
columns=('Nr.', 'Table data','Ref','Formula','Name'))
df['Formula'] = df.lookup(df.index,df['Nitrogen'])
however matching a string with a value in this dataframe seems to be giving a KeyError every time.