0

I calculated a frequency dataframe. However I would need rename the first column (there is no name, as shown below).

                    Frequency
Trump won election 46
US working vaccine  45
seqirus coronavirus vitamine    45
... ...
Apple closed shops  1
still water helps   1

created by

freq = sum(sparse_matrix).toarray()[0]
df=pd.DataFrame(freq, index=word_vectorizer.get_feature_names(), columns=['Frequency'])
df=df.sort_values(by='Frequency', ascending=False)

I have tried with

df = df.rename(columns={'index':'Words'})

But nothing has changed. My expected output would be:

Words                  Frequency
Trump won election 46
US working vaccine  45
seqirus coronavirus vitamine    45
... ...
Apple closed shops  1
still water helps   1

Any help would be appreciated. Thank you

2 Answers2

0

Use this:

df.index.names = ['Words']
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • Thank you so much @Mayank Porwal. Would it be the same in case I would need to edit more columns? –  May 24 '20 at 13:59
  • 1
    No, this only works for the index. For renaming other columns, you need to use `rename` command. – Mayank Porwal May 24 '20 at 14:01
0

Change the rename to rename_axis

df = df.rename_axis('Words')
BENY
  • 317,841
  • 20
  • 164
  • 234