Skip to main content
fix formatting
Source Link
Konrad Rudolph
  • 547k
  • 141
  • 960
  • 1.2k

Let's say this is your dataframe.

enter image description here

You can rename the columns using two methods.

  1. Using dataframe.columns=[#list]dataframe.columns=[#list]

     df.columns=['a','b','c','d','e']
    

    dfenter image description here

    The limitation of this method is that if one column has to be changed, full column list has to be passed.columns=['a' Also,'b' this method is not applicable on index labels. For example,'c' if you passed this:

     df.columns = ['a','b','c','d']
    

    This will throw an error. Length mismatch: Expected axis has 5 elements,'d' new values have 4 elements.

  2. Another method is the Pandas rename() method which is used to rename any index,'e'] column or row

     df = df.rename(columns={'$a':'a'})
    

    enter image description here

enter image description here

The limitation of this method is that if one column has to be changed, full column list has to be passed. Also, this method is not applicable on index labels. For example, if you passed this:

df.columns = ['a','b','c','d']

This will throw an error. Length mismatch: Expected axis has 5 elements, new values have 4 elements.

  1. Another method is Pandas rename() method which is used to rename any index, column or row

    df = df.rename(columns={'$a':'a'})

enter image description here

Similarly, you can change any rows or columns.

Let's say this is your dataframe.

enter image description here

You can rename the columns using two methods.

  1. Using dataframe.columns=[#list]

    df.columns=['a','b','c','d','e']

enter image description here

The limitation of this method is that if one column has to be changed, full column list has to be passed. Also, this method is not applicable on index labels. For example, if you passed this:

df.columns = ['a','b','c','d']

This will throw an error. Length mismatch: Expected axis has 5 elements, new values have 4 elements.

  1. Another method is Pandas rename() method which is used to rename any index, column or row

    df = df.rename(columns={'$a':'a'})

enter image description here

Similarly, you can change any rows or columns.

Let's say this is your dataframe.

enter image description here

You can rename the columns using two methods.

  1. Using dataframe.columns=[#list]

     df.columns=['a','b','c','d','e']
    

    enter image description here

    The limitation of this method is that if one column has to be changed, full column list has to be passed. Also, this method is not applicable on index labels. For example, if you passed this:

     df.columns = ['a','b','c','d']
    

    This will throw an error. Length mismatch: Expected axis has 5 elements, new values have 4 elements.

  2. Another method is the Pandas rename() method which is used to rename any index, column or row

     df = df.rename(columns={'$a':'a'})
    

    enter image description here

Similarly, you can change any rows or columns.

Source Link

Let's say this is your dataframe.

enter image description here

You can rename the columns using two methods.

  1. Using dataframe.columns=[#list]

    df.columns=['a','b','c','d','e']

enter image description here

The limitation of this method is that if one column has to be changed, full column list has to be passed. Also, this method is not applicable on index labels. For example, if you passed this:

df.columns = ['a','b','c','d']

This will throw an error. Length mismatch: Expected axis has 5 elements, new values have 4 elements.

  1. Another method is Pandas rename() method which is used to rename any index, column or row

    df = df.rename(columns={'$a':'a'})

enter image description here

Similarly, you can change any rows or columns.