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 lacking column names, requiring you to add them after reading the CSV data into a DataFrame.
In this article, I will explain adding or assigning column names to a DataFrame through various examples. The column names on DataFrame are used to recognize what type of data each column holds.
Key Points –
- You can specify column names when you create a DataFrame using the
columnsparameter. - Column names must be provided as a list, and the number of names must match the number of columns in the data.
- If you read data from a CSV file without headers, you can assign column names using the
namesargument inpd.read_csv(). - When using
read_csv()withnames, Pandas treats the first row of data as a regular row, not as headers. - You can add or modify column names for an existing DataFrame by setting the
df.columnsattribute to a list of names. - Column names can be any hashable value, but they are usually strings for clarity and convenience.
<a id="h-quick-examples-of-add-column-names"></a>
Quick Examples of Add Column Names
Following are quick examples of adding/assigning or setting column labels to Pandas 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
<a id="h-add-column-names-to-dataframe"></a>
Add Column Names to DataFrame
When manually creating a pandas DataFrame from a data object, you have the option to add column names. In order to create a DataFrame, utilize the DataFrame constructor, which includes a columns parameter for assigning names. This parameter accepts a list as its value, ensuring that the number of values in the list matches the number of columns in the 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.
<a id="h-add-names-while-reading-csv"></a>
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)
<a id="h-add-column-names-to-existing-dataframe"></a>
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
<a id="h-complete-examples"></a>
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)
<a id="h-faqs-on-add-column-names-to-dataframe"></a>
FAQs on 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 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.
<a id="h-conclusion"></a>
Conclusion
In conclusion, we've explored various methods of adding column names to pandas DataFrames. Whether it's during DataFrame creation, when reading from a CSV file, or for an existing DataFrame, pandas provide flexible options for organizing and manipulating your data efficiently.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Pandas Join Two DataFrames
- Join Two pandas DataFrames
- Pandas Correlation of Columns
- Pandas Add Column to DataFrame
- Pandas Add Header Row to DataFrame
- Pandas Join DataFrames on Columns
- Pandas Filter DataFrame Rows on Dates
- How to Append Two pandas DataFrames
- Differences Between pandas Join vs Merge
- How to Rename Columns With List in Pandas
- Pandas Read Multiple CSV Files into DataFrame
