I have a pandas DataFrame
, called d
, that looks like this:
0 {'energies': [[0, 100, 1, 17.537183749262393, ...
1 {'energies': [[0, 100, 1, 0.5002560191707848, ...
2 {'energies': [[0, 100, 1, 0.833607984160617, 0...
3 {'energies': [[0, 100, 1, 2.0271986592908204, ...
4 {'energies': [[0, 100, 1, 0.20764045065127334,...
...
119 {'energies': [[0, 100, 1, 0.6830323011861984, ...
120 {'energies': [[0, 100, 1, 3.9571018617595075, ...
121 {'energies': [[0, 100, 1, 4.815231137572439, 4...
122 {'energies': [[0, 100, 1, 33.556514163996766, ...
123 {'energies': [[0, 100, 1, 5.467475908262811, 5...
Name: results, Length: 124, dtype: object
How do I get rid of the dictionary? Have tried d['energies']
, but get this error:
Traceback (most recent call last):
File "C:\LinearRegresseionEENX15\LinearRegression.py", line 15, in <module>
print(d['energies'])
File "C:\LinearRegresseionEENX15\lib\site-packages\pandas\core\series.py", line 958, in __getitem__
return self._get_value(key)
File "C:\LinearRegresseionEENX15\lib\site-packages\pandas\core\series.py", line 1069, in _get_value
loc = self.index.get_loc(label)
File "C:\LinearRegresseionEENX15\lib\site-packages\pandas\core\indexes\range.py", line 389, in get_loc
raise KeyError(key)
KeyError: 'energies'
I can access the individual lists by using d[0]['energies']
. But I want to do this for the whole dataframe.
CodePudding user response:
Your question is similar to this one: Split / Explode a column of dictionaries into separate columns with pandas.
Pandas has a json_normalize() function that splits the dictionary out into its own columns. Assuming your dictionary column is the first column in your dataframe:
df2 = pd.json_normalize(df[0])