Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dictionaries and DataFrames: How do I extract the dictionary out of my DataFrame?

I have the following DataFrame:

response = requests.get(url)

data = response.json()

data1 = data['data']
rates = data1['rates']

rates_dic = rates.items()



df = pd.DataFrame(rates_dic)
df
    0   1
0   2021-10-12  {'ALU': 12.079170589589772}
1   2021-10-13  {'ALU': 11.956622225001931}
2   2021-10-14  {'ALU': 12.121163577236537}
3   2021-10-15  {'ALU': 11.869139327254496}
4   2021-10-16  {'ALU': 11.660670316092029}
...     ...     ...
345     2022-10-07  {'ALU': 13.505557425207915}
346     2022-10-08  {'ALU': 13.677978504496293}
347     2022-10-09  {'ALU': 13.677978504496293}
348     2022-10-10  {'ALU': 13.668344227796029}
349     2022-10-11  {'ALU': 13.83150297856386}

350 rows × 2 columns

What I want is to have in column 1 just the number, f.e. in row 0: 12.079170589589772, instead of {‘ALU’: 12.079170589589772}.

Is this possible and if so, how?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks a lot in advance

>Solution :

Just do this,

df[1] = [x['ALU'] for x in df[1]]

My output is a column of floats. This only works because all the keys are ‘ALU’.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading