You can add column names to pandas at the time of creating DataFrame or assign them after creating. Sometimes you might receive a CSV file that doesn’t have names and you would need to add after reading CSV data into DataFrame.
In this article, I will explain how to add/set/assign column names to DataFrame with several examples. The column names on DataFrame are used to identify what type of data each column holds.
1. Quick Examples of Pandas Add Column Names
If you are in a hurry, below are some quick examples of how to add/assign or set column labels to DataFrame.
# Quick examples of pandas add column names
# Example 1: Column names to be added
column_names=["Courses","Fee",'Duration']
# Example 2: Create DataFrame by assigning column names
df=pd.DataFrame(technologies, columns=column_names)
# Example 3: Add column names while reading a CSV file
df = pd.read_csv('courses.csv', names=column_names)
# Example 4: Add column names to existing DataFrame
df.columns = column_names
2. Add Column Names to DataFrame
You can add column names to pandas DataFrame while creating manually from the data object. In order to create a DataFrame, you would use a DataFrame constructor which takes a columns
param to assign the names. It takes a list as a value and the number of values in a list should not exceed the number of columns in DataFrame.
# Import pandas library
import pandas as pd
technologies = [ ["Spark",20000, "30days"],
["Pandas",25000, "40days"],
]
# Column names to be added
column_names=["Courses","Fee",'Duration']
# Create DataFrame by assigning column names
df=pd.DataFrame(technologies, columns=column_names)
print("Create DataFrame:\n",df)
Yields below output.
2. Add Names While Reading CSV
pandas read_csv()
method has an option to identify the column names that are presented in a CSV file, In case your CSV file doesn’t have on the first row then you can add custom names while reading a CSV into Pandas DataFrame.
# Column names to be added
column_names=["Courses","Fee",'Duration']
# Add column names while reading a CSV file
df = pd.read_csv('courses.csv', names=column_names)
3. Add Column Names to Existing DataFrame
In the above sections, you have seen how to add while creating a DataFrame. Sometimes it’s not possible to know the column names up-front and you may need to add names to the existing DataFrame.
In this example, the columns
attribute is used to assign a new list of column names (column_names
) to the DataFrame.
import pandas as pd
# Create DataFrame with out column names
df=pd.DataFrame([ ["Spark",20000, "30days"],
["Pandas",25000, "40days"],
])
# Assign column names to Existing DataFrame
column_names=["Courses","Fee",'Duration']
df.columns = column_names
print("\nDataFrame with updated column names:")
print(df)
Yields below output.
# Output:
DataFrame with updated column names:
Courses Fee Duration
0 Spark 20000 30days
1 Pandas 25000 40days
4. Complete Examples
Below is the complete example of how to assign column names to DataFram.
# Import pandas Library
import pandas as pd
technologies = [ ["Spark",20000, "30days"],
["Pandas",25000, "40days"],
]
# Column names to be added
column_names=["Courses","Fee",'Duration']
# Create DataFrame by assigning column names
df=pd.DataFrame(technologies, columns=column_names)
print(df)
# Add column names while reading a CSV file
df = pd.read_csv('courses.csv', names=column_names)
# Create DataFrame with out column labels
df=pd.DataFrame([ ["Spark",20000, "30days"],
["Pandas",25000, "40days"],
])
# Assign column names to existing DataFrame
column_names=["Courses","Fee",'Duration']
df.columns = column_names
print(df)
Frequently Asked Questions on Pandas Add Column Names to DataFrame
To add column names to a Pandas DataFrame while creating it from a dictionary, you can use the columns
parameter of the pd.DataFrame
constructor.
If you already have a DataFrame and want to change the column names, you can directly assign a new list of column names to the columns
attribute of the DataFrame.
To add a new column to an existing DataFrame with a specific name and values, you can use the assign
method. For example, the assign
method is used to add a new column named ‘Column3’ with the specified values to the existing DataFrame. The resulting DataFrame will have the original columns along with the new one.
If your CSV file doesn’t have a header, and you want to specify column names while reading it into a Pandas DataFrame, you can use the names
parameter of the read_csv
function.
If your CSV file has a header (i.e., the first row contains column names), you can simply use the read_csv
function without specifying the names
parameter. The read_csv
function automatically uses the first row of the CSV file as column names.
Conclusion
In this article, you have learned to assign column names to pandas DataFrame, while creating, when reading a CSV and to an existing DataFrame.
Happy Learning!!
Related Articles
- Pandas Join Two DataFrames
- pandas Outer Join Two DataFrames
- pandas Left Join two DataFrames
- Join Two pandas DataFrames
- Pandas Correlation of Columns
- Pandas Filter DataFrame Rows on Dates
- How to Append Two pandas DataFrames
- Pandas Add Header Row to DataFrame
- Pandas Join DataFrames on Columns
- Differences Between pandas Join vs Merge
- How to Rename Columns With List in Pandas
- Pandas Read Multiple CSV Files into DataFrame