-1

I created one new data frame by using one list and column value, and I successfully renamed the Index name but I'm not able to rename the first column name, I tried all the possible methods that I know (I want to rename this column name O with date, I tried all methods but it won't work as you can see in code snap)

datebucket=[]
def Walmart(data,stateAbb):
    Walmart_df=pd.DataFrame(data)
    Walmart_df=Walmart_df[Walmart_df['STRSTATE']== stateAbb]

    date=Walmart_df.sort_values(by='date_super').groupby(['STRSTATE','date_super']) 
    ['date_super']
    test=date.first().index
    for i in test:
        datebucket.append(i[1])

    cumsum=Walmart_df.groupby(['STRSTATE','date_super']).count()['storenum']
    NewDf=pd.DataFrame(datebucket,cumsum)   
    NewDf.index.names = ['cumsum']


    NewDf.rename(columns = {'0':'date'}, inplace = True)
    NewDf.rename(columns={NewDf.columns[0]: 'new'})

    NewDf.dropna()
    display(NewDf)


Walmart(df,'TX') 

I want to rename this column name O with date , I tried all methods but it wont work as you can see in code snap

Soumendra
  • 1,518
  • 3
  • 27
  • 54
Milan
  • 46
  • 7

1 Answers1

0

I tried to reproduce your case reading a .csv file, but I was able to rename the column.

It does seem to me that you'll catch the problem here:

NewDf.rename(columns = {'0':'date'}, inplace = True)

A good trick to debug this, according to the documentation would be to add the argument errors="raise", so try to do this:

NewDf.rename(columns = {'0':'date'}, inplace = True, errors= 'raise')

It will return you some clearer error message that will be way easier for you to understand.

Andrea S.
  • 71
  • 5