Python Forum
new help with dictionary and dataframe iteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
new help with dictionary and dataframe iteration
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to populate a dataframe thru line iteration ? knob 0 1,006 May-05-2022, 12:48 AM
Last Post: knob
  Pandas dictionary dataframe help michaelserra 4 2,204 Jun-19-2021, 10:26 AM
Last Post: michaelserra
  Dictionary iteration and creation a new dictionary from duplicates xrsxlnx 2 2,141 Mar-30-2020, 10:43 AM
Last Post: xrsxlnx
  RuntimeError: dictionary changed size during iteration Shreeniket987 3 3,747 Jun-01-2019, 01:22 PM
Last Post: buran
  RuntimeError: dictionary changed size during iteration anna 4 3,514 Feb-20-2019, 11:04 AM
Last Post: anna
  RuntimeError: dictionary changed size during iteration Skaperen 1 9,152 Dec-10-2018, 10:14 PM
Last Post: nilamo
  Why does my code not execute? (def function with 'for in' iteration over a dictionary Placebo 3 2,937 Oct-19-2018, 01:32 PM
Last Post: Placebo
  nested dictionary iteration saisankalpj 8 4,164 Sep-02-2018, 07:27 PM
Last Post: ichabod801
  dictionary, list and iteration Annie 2 3,752 Jan-08-2017, 10:44 AM
Last Post: Annie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020