Pandas Convert Row to Column Header in DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023

Sometimes you may have a header(column labels) as a row in pandas DataFrame and you would need to convert this row to a column header. Converting row to column takes the value of each element in the row and sets it as the header of that column. In this article, I will explain several ways of how to convert row to column header in pandas DataFrame using functions like DataFrame.columns(), DataFrame.iloc[] DataFrame.rename(), and DataFrame.values[] method with examples.

1. Quick Examples of Convert Row to Column Header in DataFrame

If you are in a hurry, below are some quick examples of how to convert row to column header (column labels) in pandas DataFrame.


# Below are quick example

# Assign row as column headers
df.columns = df.iloc[0]

# Using DataFrame.rename()
df2 = df.rename(columns=df.iloc[1])

# Convert row to header and remove the row
df2 = df.rename(columns=df.iloc[0]).loc[1:]

# Using DataFrame.rename() to convert row to column header
df.rename(columns=df.iloc[1], inplace = True)

# Using DataFrame.values[]
header_row = df.iloc[0]
df2 = pd.DataFrame(df.values[1:], columns=header_row)

Now, let’s create a DataFrame from list with a few rows and columns, execute these examples and validate results. Our DataFrame doesn’t contain column names but they are present as a first row on DataFrame.


# Create pandas DataFrame from List
import pandas as pd
technologies = [ ["Courses","Fee", "Duration"], 
                 ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]
df=pd.DataFrame(technologies)
print(df)

Yields below output.


# Output:
         0      1         2
0  Courses    Fee  Duration
1    Spark  20000    30days
2   Pandas  25000    40days

2. Use DataFrame.columns() to Convert Row to Column Header

You can use df.columns=df.iloc[0] to set the column labels by extracting the first row. In pandas, the index starts from 0 hence 0 means first row.


# Assign row as column headers
header_row = 0
df.columns = df.iloc[header_row]
print(df)

# Convert row to column header using DataFrame.iloc[]
df.columns = df.iloc[0]
print(df)

Yields below output. Please note that the first row on the below result is a column header with index name (0).


# Output:
0  Courses    Fee  Duration
0  Courses    Fee  Duration
1    Spark  20000    30days
2   Pandas  25000    40days

Running df.columns results below output.


# Output:
Index(['Courses', 'Fee', 'Duration'], dtype='object', name=0)

3. Using DataFrame.values[] Method

If you notice the above result, the DataFrame still has the column labels as a data row, you can remove this after converting it to a column header. This can be achieved by creating a DataFrame from the existing one.


# Using DataFrame.values[]
df=pd.DataFrame(technologies)
header_row = df.iloc[0]
df2 = pd.DataFrame(df.values[1:], columns=header_row)
print(df2)

Yields below output.


# Output:
0 Courses    Fee Duration
0   Spark  20000   30days
1  Pandas  25000   40days

4. Convert Row to Column Header Using DataFrame.rename()

You can use DataFrame.rename() to rename the header and use loc[] or iloc[] to remove the first row from the data. Use this approach even if you wanted to convert the middle or any nth row to a column header.


# Using DataFrame.rename()
df=pd.DataFrame(technologies)
df2 = df.rename(columns=df.iloc[0]).loc[1:]
print(df2)

# Using DataFrame.rename() to convert row to column header
# This doens't remove the first row.
df=pd.DataFrame(technologies)
df.rename(columns=df.iloc[0], inplace = True)

Yields below output.


# Output:
  Courses    Fee Duration
1   Spark  20000   30days
2  Pandas  25000   40days

5. Complete Example For Convert Row to Column Header


# Create pandas DataFrame from List
import pandas as pd
technologies = [ ["Courses","Fee", "Duration"], 
                 ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]
df=pd.DataFrame(technologies)
print(df)

# Assign row as column headers
header_row = 0
df.columns = df.iloc[header_row]
print(df)

# Convert row to column header using DataFrame.iloc[]
df=pd.DataFrame(technologies)
df.columns = df.iloc[0]
print(df)

# Using DataFrame.values[] & DataFrame()
df=pd.DataFrame(technologies)
header_row = df.iloc[0]
df2 = pd.DataFrame(df.values[1:], columns=header_row)
print(df2)

# Using DataFrame.rename()
df=pd.DataFrame(technologies)
df2 = df.rename(columns=df.iloc[0]).loc[1:]
print(df2)

# Using DataFrame.rename() to convert row to column header
df=pd.DataFrame(technologies)
df.rename(columns=df.iloc[0], inplace = True)
print(df)

Conclusion

In this article, you have learned how to convert row values to column header in DataFrame using DataFrame.columns(), DataFrame.rename(), DataFrame.iloc[], and DataFrame.values[] functions with examples.

Happy Learning !!

References

Naveen

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

You are currently viewing Pandas Convert Row to Column Header in DataFrame