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

Question:

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?

Thanks a lot in advance

Asked By: langermc

||

Answers:

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’.

Answered By: anarchy
df = df.join(df['1'].apply(pd.Series)) 

or

df = df.join(df.iloc[:,1:2].apply(pd.Series))
Answered By: Eren Han