0

By using the following code, I have been able to transform my dataframe df (shown in attached Picture1) into dictionary of subset dataframes (split on basis of rows sharing same Year, Month, Week). Resultant dictionary is shown in Picture2.

dict_df = dict(tuple(df.groupby(['Year', 'Month', 'Week'])))
print(dict_df)

Next, I am trying to reference elements in Open column in dict_df by using the following code and the output is in Photo3:

for item in dict_df.keys():
    print(dict_df[item]['Open'])
    print(type(dict_df[item]['Open']))

Question:

How do I reference to any item (say, 0th or 1st or any ) within the Open column in Picture3? I tried print(dict_df[item]['Open'][0]) but I get error get_loc raise KeyError(key) from err KeyError: 0

enter image description here enter image description here enter image description here

Sinha
  • 431
  • 1
  • 5
  • 12
  • 2
    Use `iloc` like `print(dict_df[item]['Open'].iloc[0])` – jezrael Apr 20 '22 at 05:15
  • @jezrael `.iloc[0]` works. How do I change the datatype of such reference item to float (its string presently)? I tried `dict_df[item]['Open'].iloc[0].astype(float)` but get error. Any other way to smartly convert into float? – Sinha Apr 20 '22 at 18:43

0 Answers0