The problem
I am facing a problem as I am managing a dataset each of which entry has associated a dictionary in the form of
dictionary = {
'Step_1': {
'Q':'123',
'W':'456',
'E':'789'
},
'Step_2': {
'Q':'753',
'W':'159',
'E':'888'
}
}
please note that the dicts have a variable number of Step
s
So I am organising the data into a pandas df like:
dicts
0 {'Step_1': {'Q': '123', 'W': '456', ...
1 {'Step_1': {'Q': '123', 'W': '456', ...
2 {'Step_1': {'Q': '123', 'W': '456', ...
and would like now to do some row-wise operations, like getting each dict['Step_1']['Q']
value.
I know that it's generally suggested to not work with dicts as df values, so I'd like to use a good, pythonic (read: fast) solution.
How would you proceed to get each dict['Step_1']['Q']
row-wise?
What I tried
A simple solution that came to my mind was:
df[dicts]['Step_1']['Q']
, but it doesn't seem to work. (Why? Might it be because this way pandas doesn't "unpack" the row values, hence cannot access the dicts?)
A more complex solution that I found to work is to use a function to access the data, as follows:
def access(x):
return (x["Step_1"]["V"])
df['new_col'] = df['dicts'].apply(lambda x: access(x))
but I don't quite like this solution. As far as I know, using the apply method is not the optimal way to tackle the problem.