I'm not sure if this is a dumb way to go about things, but I've got several data frames, all of which have identical columns. I need to rename the columns within each to reflect the names of each data frame (I'll be performing an outer merge of all of these afterwards).
Let's say the data frames are called df1
, df2
and df3
, and each contains the columns name
, date
, and count
.
I'd like to rename each of the columns in df1
into name_df1
, date_df1
, and count_df1
.
I've written a function to rename the columns, thus:
df_list=[df1, df2, df3]
def rename_cols():
col_name="name"+suffix
col_count="count"+suffix
col_date="date"+suffix
for x in df_list:
if x['name'].tail(1).item() == df1['name'].tail(1).item():
suffix="_"+"df1"
rename_cols()
continue
elif x['name'].tail(1).item() == df2['name'].tail(1).item():
suffix="_"+"df2"
rename_cols()
continue
else:
suffix="_"+"df3"
rename_cols()
col_names=[col_name,col_date,col_count]
x.columns=col_names
Unfortunately, I get the following error: KeyError: 'name'
I'm really struggling to figure out why that's going on. The columns for df1, the first data frame in the df_list
, gets renamed. Everything else stays the same... Am I messing up basic syntax (probably), or is there a fundamental misunderstanding that I've got of how things should work?
From what I can ascertain, the first data frame in the list is being iterated through more than once — but why would that be the case?