Home > Software engineering >  Python - adding a column via a loop
Python - adding a column via a loop

Time:03-22

I am relatively new to python and I am currently trying to make my life easier by exploring loops.

I have several data sets and I wish to concatenate them but before I do, I want to add a column with the original data set name.

previously I have done this individually:

df1["location"]= "Norfolk"

df2["location"]= "Devon"

This time have a large number of data frames I need to work with.

frames=[df1,df2,df3,df4...]

for i in frames:
       i["location"]=i

This added the column but instead of filling it with the respective name, for example "df1", it added all the column headers. I tried with str(i) and nothing happened.

I have tried various other methods using a mix of stackflow answers for related problems but still haven't been able to do this. These include creating the empty column and trying various appends, using lists and dictionaries.

I just can't seem to get this to work.

I think I was being nieve thinking it would be this simple. Does anyone have any ideas? or any 'best working practices ' for this sort of this?

If it helps the column fills are the same as the df name. ie df1['location'] should be filled with "df1"

CodePudding user response:

You cannot assign i to i["location"] as it is a string. It is a dataframe object. You need to have another list of names for each dataframe. You could also generate this dynamically in your program wherever you are initializing the dataframe.

So something like:

frames = [df1, df2, df3, df4, ...]
frame_names = ['df1', 'df2', 'df3', 'df4', ...]

for i in range(len(frames)):
    frames[i]["location"] = frame_names[i]
  • Related