Renaming Pandas Data Frame Columns Using A For Loop
Solution 1:
I guess you can achieve this with something simplier, like that :
df_list=[df1, df2, df3]
for i, df inenumerate(df_list, 1):
df.columns = [col_name+'_df{}'.format(i) for col_name in df.columns]
If your DataFrames have prettier names you can try:
df_names=('Home', 'Work', 'Park')
for df_name in df_names:
df = globals()[df_name]
df.columns = [col_name+'_{}'.format(df_name) for col_name in df.columns]
Or you can fetch the name of each variable by looking up into globals()
(or locals()
) :
df_list = [Home, Work, Park]
fordfin df_list:
name = [k for k, v in globals().items() ifid(v) == id(df) and k[0] != '_'][0]
df.columns = [col_name+'_{}'.format(name) for col_name in df.columns]
Solution 2:
My preferred rather simple way of doing this, especially when you want to apply some logic to all column names is:
for col in df.columns:
df.rename(columns={col:col.upper().replace(" ","_")},inplace=True)
Solution 3:
I'll suppose that you have your stored in a dictionary as this is the idiomatic way of storing a series of named objects in Python. The idiomatic pandas way of changing your column names is to use a vectorised string operation on df.columns
:
df_dict = {"df1":df1, "df2":df2, "df3":df3}
for name, df in df_dict.items():
df.columns = df.columns + "_" + name
Another option to consider is adding the suffixes automatically during the merge. When you call merge
you can specify the suffixes that will be appended to duplicate column names with the suffixes
parameter. If you just want to append the names of the dataframes, you can call it like this. :
from itertools import reduce
df_merged = reduce(lambda x,y: ("df_merged",
x[1].merge(y[1], left_index=True, right_index=True,
suffixes = ("","_"+y[0]))),
df_dict.items())[1]
Solution 4:
For completeness, since nobody has mentioned df.rename
, see Andy Hayden's answer here:
df.rename
can take a function as an argument, so in this case:
df_dict = {'df1':df1,'df2':df2,'df3':df3}
for name,df in df_dict.items():
df.rename(lambdax: x+'_'+name, inplace=True)
Post a Comment for "Renaming Pandas Data Frame Columns Using A For Loop"