Home > Software engineering >  Issues with key values within functions
Issues with key values within functions

Time:05-24

I have a dictionary of dataframes and i wanted to loop through these and add a column to just put the key/name of the dataframe in to the column for reference. I can manage this outside of a function fine:

k = 'df1'
df_dict[k]['Source label'] = "source"

however if i put this inside a function like this:

def do_all_dfs():
    for k,v in df_dict.items():
        print(k)
        df_dict[k]['Source label'] = "source"

It will recognise and print the label ok, and it's type string, but it throws an error on trying to add the column:

ValueError: could not convert string to float: 'source'

I think there must be something fundamental i don't understand here, because i had a similar issue with another function where I got a NameError when using k this way - fine outside of a function, but when i put it inside one it throws an error.

There are 4 dataframes in the dictionary and all pretty much identical. As i'm just adding a column to each, i can't see what's going wrong.

What am I missing?

CodePudding user response:

use keys, instead of .items, and you do not need v variable in the loop

def do_all_dfs():
    for k in df_dict.keys():
        print(k)
        df_dict[k]['Source label'] = "source"
  • Related