Table of Contents
Adding a header row to a Pandas DataFrame is a simple process that involves using the built-in function “columns” to specify the column names and then using the “rename” function to assign these names as the header row. This allows for easy navigation and organization of the data within the DataFrame. Additionally, the header row can be customized by specifying the desired names or by importing a list of names from an external source. Overall, adding a header row to a Pandas DataFrame enhances the readability and functionality of the data.
Add Header Row to Pandas DataFrame (With Examples)
You can use one of the following three methods to add a header row to a pandas DataFrame:
#add header row when creating DataFrame df = pd.DataFrame(data=[data_values], columns=['col1', 'col2', 'col3']) #add header row after creating DataFrame df = pd.DataFrame(data=[data_values]) df.columns = ['A', 'B', 'C'] #add header row when importing CSV df = pd.read_csv('data.csv', names=['A', 'B', 'C'])
The following examples show how to use each of these methods in practice.
Example 1: Add Header Row When Creating DataFrame
The following code shows how to add a header row when creating a pandas DataFrame:
import pandas as pd import numpy as np #add header row when creating DataFrame df = pd.DataFrame(data=np.random.randint(0, 100, (10, 3)), columns =['A', 'B', 'C']) #view DataFrame df A B C 0 81 47 82 1 92 71 88 2 61 79 96 3 56 22 68 4 64 66 41 5 98 49 83 6 70 94 11 7 1 6 11 8 55 87 39 9 15 58 67
Example 2: Add Header Row After Creating DataFrame
The following code shows how to add a header row after creating a pandas DataFrame:
import pandas as pd import numpy as np #create DataFramedf = pd.DataFrame(data=np.random.randint(0, 100, (10, 3))) #add header row to DataFrame df.columns = ['A', 'B', 'C'] #view DataFrame df A B C 0 81 47 82 1 92 71 88 2 61 79 96 3 56 22 68 4 64 66 41 5 98 49 83 6 70 94 11 7 1 6 11 8 55 87 39 9 15 58 67
Example 3: Add Header Row When Importing DataFrame
The following code shows how to add a header row using the names argument when importing a pandas DataFrame from a CSV file:
import pandas as pd import numpy as np #import CSV file and specify header row names df = pd.read_csv('data.csv', names=['A', 'B', 'C']) #view DataFrame df A B C 0 81 47 82 1 92 71 88 2 61 79 96 3 56 22 68 4 64 66 41 5 98 49 83 6 70 94 11 7 1 6 11 8 55 87 39 9 15 58 67
Cite this article
stats writer (2024). How can we add a header row to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-we-add-a-header-row-to-a-pandas-dataframe/
stats writer. "How can we add a header row to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-we-add-a-header-row-to-a-pandas-dataframe/.
stats writer. "How can we add a header row to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-we-add-a-header-row-to-a-pandas-dataframe/.
stats writer (2024) 'How can we add a header row to a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-we-add-a-header-row-to-a-pandas-dataframe/.
[1] stats writer, "How can we add a header row to a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can we add a header row to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
