Mission 344: Combining data using pandas; Problem using a for loop to add columns to a list of dataframes

Screen Link:

My Code:
In this screen we are asked to add a new column to three dataframes which we have created from .csv files.
I created a list of the dataframe names and then wanted to loop over them to add a new column to each dataframe but python apparently reads each element in the list as a string within the loop instead of taking it as a dataframe name; outside of the loop the the name will access the dataframe;

I am unable, at this time, to get python to read the df inside the loop and add a new column to it.

happy_df_list = ['happiness2015','happiness2016','happiness2017']

for df in happy_df_list:
    df['Year'] = happy_df_list[-4:]

What I expected to happen:


What actually happened: 

Replace this line with the output/error


<!--Enter other details below: -->
the screenshot shows the error that is displayed and the other one shows one of the dataframes working correctly.

I was wondering if someone can guide me; thanks

Hi saleem,

You are close, but a couple of things. The dataframes are objects, not strings, what this means in this situation is that the dataframe names in your happy_df_list do not need parenthesis around them.

In line 4 of your code, within your for loop, the left side of the statement is correct (given that you fix the above issue), but I’m not quite sure what you’re trying to accomplish on the right side of the statement. It seems like you are trying to extract the year from the string, but that’s not exactly what your code is doing. If you want to try looping to solve this you either need to create a 1-2 more additional lists for your dataframe objects and the year string values and/or modify the way you are extracting the last 4 values from the dataframe names.

Hope this helps give you some avenues to explore.

Hello Kevin,

Thank you for the reply;
the task for the screen required that we add a new column to each of the three dataframes created from the three csv files.
The new column was to take have the label ‘Year’ and take the value of the year; the right side was trying to extract the year from the variable name used in the list given above.
the aim of the for loop was to take the name of the dataframe from the list, create a new column in each dataframe and assign the year as a value to each. This way I’d not need to write out the code to add a new column to the dataframes one at a time.

happiness2015['Year'] = 2015

generic:
dataframe_name[‘Year’] = 9999
with the year 9999 extracted from the dataframe name.

Hi @saleemdar.dr,

If you feel that  your question was successfully solved, could you please mark the helpful answer as Solution? You can find here how to do it technically (in the GUIDELINE #5). Please consider this practice also for any other questions you might ask in the Community. This would be of great help both to your helpers and the other learners who can have the same questions in the future.

Many thanks and happy learning! 

NOTE: This is an automated message.

Hello,

unfortunately my question was not answered and I was not able to use dataframes names in the manner I wanted.