Skip to content Skip to sidebar Skip to footer

Renaming Columns Of A Pandas Dataframe Without Column Names

I'm trying to name the columns of my new dataframe after the dataframe.from_dict operation. Simply using pandas.dataframe.from_dict function: df = pd.DataFrame.from_dict(my_dict,or

Solution 1:

If you want the index as the keys in your dict, you don't need to rename it.

df = pd.DataFrame.from_dict(dicts, orient = 'index') #index is namedf.columns = (['number']) #non-index column is numberdf.index.name = 'name'

Or instead of changing the index name you can make a new column:

df = df.reset_index() #named column becomes index, index becomes ordered sequencedf['name'] = df['index'] #new column with names

del df['index'] #delete old column

Solution 2:

You can probably do something like this by implicitly referring the columns names and then set new names.

data = (  
  pd.DataFrame.from_dict(my_dict,orient='index')
  .rename(columns=dict(zip(df.columns,['name','number'])))
)

Post a Comment for "Renaming Columns Of A Pandas Dataframe Without Column Names"