You can create a DataFrame in Pandas with the following code. I highly recommend you This book to learn Python. Pandas DataFrame is a 2-dimensional data structure, that looks like a table with rows and columns. You will see 12 examples of the Pandas DataFrame in this article.
- Empty DataFrame in Pandas
- DataFrame in Pandas from the list
- Create a DataFrame in Python with two lists
- Create a DataFrame from an array in Python
- Create a DataFrame in Python with Column Names
- Create a DataFrame in Python from the dictionary
- Create a DataFrame in Python with random values
- Pandas DataFrame from a list of dictionaries
- Pandas DataFrame from a list of tuples
- Pandas DataFrame from list of list
- DataFrame in Pandas from CSV
- Create a DataFrame in Python from Excel
Step 1: Install Pandas Library
Install the Pandas library using this code, if it is not installed.
pip install pandas
Empty DataFrame in Pandas
# Import the Pandas library as pd import pandas as pd # Create Empty DataFrame a = pd.DataFrame() # Display the DataFrame print(a)
Output:
Empty DataFrame Columns: [] Index: []
DataFrame in Pandas from the list
# Import the Pandas library as pd import pandas as pd # Initialize a list a = [5, 12, 24, 55, 71] # Create DataFrame b = pd.DataFrame(a) # Display the DataFrame print(b)
Output:
0 0 5 1 12 2 24 3 55 4 71
Create a DataFrame in Python with two lists
# Import the Pandas library as pd import pandas as pd # Initialize two lists a = [5, 12, 24, 55, 71] b = [21, 43, 13, 78, 97] # Create DataFrame c = pd.DataFrame(zip(a,b)) # Display the DataFrame print(c)
Output:
0 1 0 5 21 1 12 43 2 24 13 3 55 78 4 71 97
Create a DataFrame from an array in Python
# Import the required libraries import pandas as pd import numpy as np # Initialize a NumPy Array a = np.array([[5, 10, 15, 40], [20, 25, 36, 55], [37, 48, 19, 70]]) # Create DataFrame df = pd.DataFrame(a) # Display the Output print(df)
Output:
0 1 2 3 0 5 10 15 40 1 20 25 36 55 2 37 48 19 70
Create a DataFrame in Python with Column Names
# Import the required libraries import pandas as pd import numpy as np # Initialize a NumPy Array a = np.array([[5, 10, 15, 75], [20, 25, 36, 85], [37, 48, 19, 17]]) # Create DataFrame df = pd.DataFrame(a, columns=['A', 'B', 'C', 'D']) # Display the Output print(df)
Output:
A B C D 0 5 10 15 75 1 20 25 36 85 2 37 48 19 17
Create a DataFrame in Python from the dictionary
# Import the Pandas library as pd import pandas as pd # Initialize a dictionary dict = {'Students':['John', 'Ahmad', 'Satish', 'Ali'], 'Scores':[71, 70, 68, 73]} # Create a DataFrame df = pd.DataFrame(dict) # Display the Output print(df)
Output:
Students Scores 0 John 71 1 Ahmad 70 2 Satish 68 3 Ali 73
Create a DataFrame in Python with random values
# Import the required libraries import pandas as pd import numpy as np # Create a Random Array of 5 rows and 3 columns # and values varies from 0 to 50 a = np.random.randint(0, 50, size=(5, 3)) # Create a DataFrame df = pd.DataFrame(a, columns = ['A', 'B', 'C']) # Display the Output print(df)
Output:
A B C 0 19 29 1 1 17 34 18 2 26 46 27 3 4 13 10 4 46 8 5
Pandas DataFrame from a list of dictionaries
# Import the Pandas library as pd import pandas as pd # Initialise list of dictionaries a = [{'A': 'John', 'B': 'Ali', 'C': 'Harry'}, {'A': 10, 'B': 20, 'C': 30}, {'A': 2.5, 'B': 3.5, 'C':4.0, 'D':3.0 }] # Creates DataFrame. df = pd.DataFrame(a) # Display the Output print(df)
Output:
A B C D 0 John Ali Harry NaN 1 10 20 30 NaN 2 2.5 3.5 4.0 3.0
Create a Pandas DataFrame from a list of tuples
# Import the Pandas library as pd import pandas as pd # Initialize a list of list a = [(35, 12, 39, 72, 47), (15, 63, 82, 18, 9), (41, 92, 14, 84, 5)] # Create DataFrame df = pd.DataFrame(a, columns=['A','B','C','D','E']) # Display the Output print(df)
Output:
A B C D E 0 35 12 39 72 47 1 15 63 82 18 9 2 41 92 14 84 5
Pandas DataFrame from list of list
# Import the Pandas library as pd import pandas as pd # Initialize a list of list a = [[5, 10, 35, 12, 7], [15, 3, 2, 8, 9], [11, 32, 34, 54, 65]] # Create DataFrame df = pd.DataFrame(a, columns=['A','B','C','D','E']) # Display the Output print(df)
Output:
A B C D E 0 5 10 35 12 7 1 15 3 2 8 9 2 11 32 34 54 65
Create a DataFrame in Pandas from CSV
# Import the Pandas library as pd import pandas as pd # Read CSV as Pandas DataFrame a = pd.read_csv('house_price.csv') # Display the first five rows of DataFrame print(a.head())
Output:
Area Rooms House_Age Price 0 5000 5 5 75000 1 4000 4 5 65000 2 3000 3 1 60000 3 2000 3 1 58000 4 1500 2 1 50000
Create a DataFrame in Python from Excel
# Import the Pandas library as pd import pandas as pd # Read Excel as Pandas DataFrame a = pd.read_excel('house_price.xlsx') # Display the first five rows of DataFrame print(a.head())
Output:
Area Rooms House_Age Price 0 5000 5 5 75000 1 4000 4 5 65000 2 3000 3 1 60000 3 2000 3 1 58000 4 1500 2 1 50000