You can add column names to the pandas Series at the time of creating or assign the name after creating. In this article, I will explain how to add a column name to a Series with several examples. The column names on the Series are used to identify what type of data each column holds.
1. Quick Examples of Add Column Name to Series
Below are some quick examples of how to add/assign column name to a Series.
# Below are quick examples
# Example 1: Add column name to Series
technologies = ["Spark", "Python", "Pandas"]
ser = pd.Series(technologies, name = 'Technology')
# Example 2: Add column name to Series
ser_df = pd.DataFrame(ser, columns = ['Technology'])
# Example 3: Add column name to Series
ser_df = pd.DataFrame({'Technology': ser.values})
# Example 4: Add column name to Series
ser_df = pd.DataFrame(ser).reset_index()
ser_df.columns = ['Index', 'Technology']
2. Add Column Name to Pandas Series
By using name param you can add a column name to Pandas Series at the time of creation using pandas.Series() function. The row labels of the Series are called the index and the Series can have only one column. A List, NumPy Array, and Dict can be turned into a pandas Series.
Let’s create Pandas Series along with its column name.
# Import pandas Library
# Create Pandas Series
import pandas as pd
technologies = ["Spark", "Python", "Pandas"]
ser = pd.Series(technologies, name = 'Technology')
print(ser)
Yields below output.
As you can see from the above, we got a column name of Series at the time of creation.
# Output:
0 Spark
1 Python
2 Pandas
Name: Technology, dtype: object
3. Add Column Names to Existing Series.
Alternatively, you can add the column name to the existing Pandas using Series.name attribute.
# Example 2: Set column name to esisting Series
technologies = ["Spark", "Python", "Pandas"]
ser = pd.Series(technologies)
ser.name='Technology'
print(ser)
Yields same output as above.
4. Add Column Name After Assigning Series to DataFrame
Series can be assigned to DataFrame and if your Series doesn’t have name then the DataFrame also doesn’t contain name. So let’s see how to fix this. Below example creates a DataFrame from the series.
# Add column name to Series
ser_df = pd.DataFrame(ser, columns = ['Technology'])
print(ser_df)
Yields below DataFrame as output.
# Output:
Technology
0 Spark
1 Python
2 Pandas
4. Add Column Name Using Dictionary
You can also add name using python dictionary. You can either use the name from the Series using Series.name or assign a custom name for key field in the dict.
# Add column name to Series
ser_df = pd.DataFrame({'Technology': ser.values})
print(ser_df)
# Output:
# Technology
# 0 Spark
# 1 Python
# 2 Pandas
5. Add Column Name to Series using reset_index
You can also have Series Index and values as two different columns on DataFrame, In order to get that use reset_index() function. reset_index() reset the index on the DataFrame.
# Add column name to Series
ser_df = pd.DataFrame(ser).reset_index()
ser_df.columns = ['Index', 'Technology']
print(ser_df)
Yields below output.
# Output:
Index Technology
0 0 Spark
1 1 Python
2 2 Pandas
5. Conclusion
In this article, you have learned how to add a column name to the pandas Series at the time of creation or after creation and also learned how to add column name to Pandas DataFrame which I have created from the Series.
Happy Learning!!
Related Articles
- How to Sort pandas Series
- Drop Rows From Pandas DataFrame Examples
- Drop Single & Multiple Columns From Pandas DataFrame
- Find Intersection Between Two Series in Pandas
- Change the Order of Pandas DataFrame Columns
- Pandas groupby() and sum() With Examples
- Difference Between loc and iloc in Pandas DataFrame
- How to convert GroupBy output from Series to DataFrame?