You can add a header in Pandas DataFrame with the following code. I highly recommend you This book to learn Python. In this article, You will see 2 examples of adding a header in Pandas DataFrame.
Step 1: Install Pandas Library
Install the Pandas library using this code, if it is not installed.
pip install pandas
Example 1
# Import the Pandas library as pd import pandas as pd # Read the Data data = pd.read_csv("house.csv") # Display the 5 rows of data print(data.head()) # Now Read the data and add header df = pd.read_csv("house.csv", names=["Area", "Rooms", "Age", "Price"]) # Display the 5 rows of DataFrame print(df.head())
Output:
5000 5 6 75000 0 4000 4 5 65000 1 3000 3 1 60000 2 2000 3 1 58000 3 1500 2 1 50000 4 7000 5 5 90000 Area Rooms Age Price 0 5000 5 6 75000 1 4000 4 5 65000 2 3000 3 1 60000 3 2000 3 1 58000 4 1500 2 1 50000
Example 2
# Import the required libraries import pandas as pd import numpy as np # Make a NumPy array a = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]]) # Make a DataFrame df = pd.DataFrame(a) # Display the DataFrame without Header print(df) # Add header to Pandas DataFrame df.columns = ["A", "B", "C"] # Display the DataFrame print(df)
Output:
0 1 2 0 1 2 3 1 4 5 6 2 7 8 9 A B C 0 1 2 3 1 4 5 6 2 7 8 9