๐ŸŒ
Medium
medium.com โ€บ @whyamit101 โ€บ pandas-set-column-names-a-comprehensive-guide-130c84f8761a
Pandas Set Column Names: A Comprehensive Guide | by why amit | Medium
April 12, 2025 - Yes, while pandas allows repeated column names, itโ€™s best practice to keep them unique to avoid confusion during data manipulation and analysis. How can I check the current column names in a DataFrame? You can check the column names of a DataFrame easily using df.columns, which returns an index object containing the list of column names. Setting and changing column names is an essential skill in data manipulation with pandas.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ python โ€บ pandas โ€บ pandas-dataframe-set-column-names
How to set Column Names for DataFrame in Pandas?
July 9, 2021 - To set column names of DataFrame in Pandas, use pandas.DataFrame.columns attribute. Assign required column names as a list to this attribute.
Top answer
1 of 16
4717

Rename Specific Columns

Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed:

df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})

# Or rename the existing DataFrame (rather than creating a copy) 
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)

Minimal Code Example

df = pd.DataFrame('x', index=range(3), columns=list('abcde'))
df

   a  b  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

The following methods all work and produce the same output:

df2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1)
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns')
df2 = df.rename(columns={'a': 'X', 'b': 'Y'}) 

df2

   X  Y  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

Remember to assign the result back, as the modification is not-inplace. Alternatively, specify inplace=True:

df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)
df

   X  Y  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x
 

You can specify errors='raise' to raise errors if an invalid column-to-rename is specified.


Reassign Column Headers

Use df.set_axis() with axis=1.

df2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1)
df2

   V  W  X  Y  Z
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

Headers can be assigned directly:

df.columns = ['V', 'W', 'X', 'Y', 'Z']
df

   V  W  X  Y  Z
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x
2 of 16
2574

Just assign it to the .columns attribute:

>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
   $a  $b
0   1  10
1   2  20

>>> df.columns = ['a', 'b']
>>> df
   a   b
0  1  10
1  2  20
Find elsewhere
๐ŸŒ
GitHub
github.com โ€บ pandas-dev โ€บ pandas โ€บ issues โ€บ 5909
set_columns() equivalent of set_index() ? ยท Issue #5909 ยท pandas-dev/pandas
January 11, 2014 - One can directly manipulate the .columns attribute of the DF, but it's often convenient to be able to alter columns in-line after some other operation--e.g., data = pd.concat([df_a, df_b], axis=1).set_columns(['a', 'b', 'c'])
Author ย  tyarkoni
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ user_guide โ€บ options.html
Options and settings โ€” pandas 3.0.3 documentation - PyData |
In [21]: with pd.option_context("display.max_rows", 10, "display.max_columns", 5): ....: print(pd.get_option("display.max_rows")) ....: print(pd.get_option("display.max_columns")) ....: 10 5 In [22]: print(pd.get_option("display.max_rows")) 60 In [23]: print(pd.get_option("display.max_columns")) 0 ยท Using startup scripts for the Python/IPython environment to import pandas and set options makes working with pandas more efficient.
๐ŸŒ
Built In
builtin.com โ€บ data-science โ€บ pandas-add-column
How to Add Columns in a Pandas DataFrame | Built In
Summary: Five methods for adding columns to a Pandas DataFrame include direct assignment, insert(), .loc[], .assign() and Python dictionary mapping. Each approach includes concise examples for quick application in data workflows. more Five methods ...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas โ€“ set order of columns in dataframe
Pandas - Set Order of Columns in DataFrame - Spark By {Examples}
June 27, 2025 - You can use set order or rearrange columns of pandas DataFrame using either loc[], iloc[], and reindex() methods. In this article, I will explain how to
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74362719 โ€บ using-set-with-pandas
python - using set() with pandas - Stack Overflow
x = pd.DataFrame(df1, colmns=[0]) set(x.iloc[:,0].values) But if you just want the unique values in column 0 then you can use