You can use the assign function:

df = df.assign(industry='yyy')
Answer from Mina HE on Stack Overflow
🌐
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.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.html
pandas.DataFrame — pandas 3.0.3 documentation - PyData |
>>> df2 = pd.DataFrame( ... np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=["a", "b", "c"] ...
🌐
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.
🌐
Saturn Cloud
saturncloud.io › blog › convert-the-first-row-of-a-pandas-dataframe-to-column-names-a-comprehensive-guide
Streamlining Data Preparation: How to Set Column Names in a Pandas DataFrame from the First Row | Saturn Cloud Blog
July 10, 2023 - We then assign this row to the .columns property, which sets the column names of the DataFrame. After converting the first row to column names, the index of the DataFrame will be off by one. To fix this, we can use the .reset_index method: ... The drop=True argument is used to avoid the old index being added as a new column in the DataFrame. And that’s it! You’ve successfully converted the first row of a pandas DataFrame to column names.
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   pandas-dev
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Add rows/columns to DataFrame with assign(), insert() | note.nkmk.me
August 1, 2023 - source: pandas_add_column.py · If you specify a non-existent column name, a new column will be added with the assigned value. When a scalar value is assigned, all elements in the column are set to that value. df['D'] = 0 print(df) # A B C D # ONE 0 B1 C1 0 # TWO 0 B2 C2 0 # THREE 0 B3 C3 0 ·
🌐
Built In
builtin.com › data-science › pandas-add-column
How to Add Columns in a Pandas DataFrame | Built In
Inserting column D in between columns A and B in pandas DataFrame. | Image: Soner Yildirim · The insert function takes three parameters that are the index, the name of the column and the values. The column indices start from zero, so we set the index parameter as one to add the new column next to column A.
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:

Copydf = 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

Copydf = 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:

Copydf2 = 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:

Copydf.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.

Copydf2 = 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:

Copydf.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:

Copy>>> 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
🌐
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
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.set_index.html
pandas.DataFrame.set_index — pandas 3.0.3 documentation
Whether to append columns to existing index. Setting to True will add the new columns to existing index.
🌐
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