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"