What is the best way to set both axes of a pandas DataFrame? I am currently using a utility function that calls set_axis
on both axes:
def DataFrame_set_axes(df, index, columns):
"""Set both the index and column axes of DataFrame."""
return df.set_axis(index, axis='index').set_axis(columns, axis='columns')
But this does not seem like the best solution. I am looking for something that can be called simply like: df.set_axes(index, columns)
.
CodePudding user response:
Maybe this?
pd.DataFrame(df.values, index=index, columns=columns)
CodePudding user response:
You could just assign the new indices/columns to the index
/columns
attributes:
df.index, df.columns = index, columns