Python Coding Help Needed Urgently

Hello folks! I need help with python coding. I have a dictionary who keys are years, and values are dataframes. See image. Now the goal is to iterate through each dataframe and assign to each row under COL-ADD which is ‘Period’, Period 01, Period 02, …, Period n for each year.

These are the variables

dictionary = year_list_dict
dictionary keys are exact values stored in >> data_year_list
<> the elements in the data_year_list are string values but can easily be converted to integers — so are the dictionary keys (I actually have a line that converts between int() and str())
column to add = ‘Period’
<> original dataframe was a dataset of multiple years which I used a .groupby() on to group each year as a dataframe assign to their respective years into a dictionary

Thanks for the much needed help!!!

The second picture is the line set I’m running.
The third picture is the result…obviously wrong.

VHS_CURR

to start with, is this a nested dictionary? your first image makes it look like you’re trying to store multiple values under one key?

I’m not super sure I understood what exactly you want to do, but I can give you some pointers for how to iterate on the years and the dataframes.

First iterate on the keys of your dictionary, and then you can iterate on the rows of the dataframe, and adjust any value there, i.e.:

for year in year_list_dict:
df = year_list_dict[year]
for row in df.iterrows():
row[‘COL-ADD’] = XXX

It wasn’t clear to me what you want to do in the COL-ADD columns, but feel free to send me a message with more details (3bien on slack).

cheers,

Ignacio

Yes it is a nested dictionary. It is a dataframe nested inside a dictionary. {dataframe nested in dictionary}

Ignacio the method brings up a TypeError: ‘tuple’ object does not support item assignment.

Are you on discord?

Try posting to https://stackoverflow.com/questions
or
https://python-forum.io/

Alright will do. Thanks Janet!

1 Like

Hey this might be a better framing of the question:

Hello folks

I am trying to iterate through a dataframe nested in a dictionary and assign period numbers in a new column to each row in the dataframe. The code below is what I have so far and what that does is assign the number of the dictionary key to all the rows in that dataframe.

Goal:

-Dictionary Key 01: Consisting of a DataFrame with 10 rows; create new column called ‘Period’ and assign Period + row number to each (Period 1, Period 2,…, Period 10)
-Dictionary Key 02: Consisting of a DataFrame with 11 rows; create new column called ‘Period’ and assign Period + row number to each (Period 1, Period 2,…, Period 11)
-…
-…
-…
-…
-Dictionary Key 07: Consisting of a DataFrame with 9 rows; create new column called ‘Period’ and assign Period + row number to each (Period 1, Period 2,…, Period 9)

for i in range(0, len(data_year_list)):
    for year in year_list_dict[data_year_list[i]].iterrows():
        year_list_dict[data_year_list[i]]['Period'] = 'Period ' + str(i)

This is spitting out:

-Dictionary Key 01: Consisting of a DataFrame with 10 rows; create new column called ‘Period’ and assign Period + row number to each (Period 1, Period 1,…, Period 1)
-Dictionary Key 02: Consisting of a DataFrame with 11 rows; create new column called ‘Period’ and assign Period + row number to each (Period 2, Period 2,…, Period 2)
-…
-…
-…
-…
-Dictionary Key 07: Consisting of a DataFrame with 9 rows; create new column called ‘Period’ and assign Period + row number to each (Period 7, Period 7,…, Period 7)

Thanks in advance for the help with this.

Your i currently represents the current year, not the current row in your data structure, and what you have named for year is actually for row. You’re also modifying the entire DataFrame rather than the individual rows, which will copy the same value you are setting into every row.

This should work, if I understand your requirements. The DataFrame object already supports setting a column to a list of values, so you just need to generate a list of “Period n” strings based on the length of the DataFrame and assign that directly. You don’t need to loop over the rows here.

for year in data_year_list: # why not just use year_list_dict.keys() ?
  df = year_list_dict[year]
  df['Period'] = ["Period " + str(i) for i in range(1,len(df)+1)]
2 Likes

You are awesome!!! It worked. I just started working with Python this summer, and jumped into this live project as a trainer. Thank you very much for your help!

While I have you hear, is it alright I keep in touch for future help and communication? I’m on discord if that works best for you.