How to Create a Dataframe From Arrays in Python
To create a dataframe from arrays in Python, you can use the numpy array.
The following example shows how to create a dataframe from arrays in Python using the numpy array.
Using numpy Array
We can use the numpy array to create a dataframe.
Suppose we have the following arrays:
import pandas as pd
import numpy as np
# Define arrays
date = np.array(['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'])
product_code = np.array(['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'])
product_name = np.array(['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'])
price = np.array([4500, 550, 250, 50, 350, 50])
status = np.array([1, 1, 1, 0, 1, 1])
# Create dataframe
df = pd.DataFrame({"Date": date, "Product_code": product_code, "Product_Name": product_name, "Price": price, "Status": status})
# Show dataframe
print(df)
Output: 👇️
Date Product_code Product_Name Price Status
0 01-03-2023 A-101 Laptop 4500 1
1 01-03-2023 A-102 Mobile 550 1
2 01-03-2023 A-103 Printer 250 1
3 01-03-2023 B-101 Keyboard 50 0
4 02-03-2023 B-102 Scanner 350 1
5 02-03-2023 B-104 Mouse 50 1
In this example, we use the numpy array to create a dataframe df. The output shows the dataframe created from the numpy arrays.
Conclusion
We can use the numpy array to create a dataframe from arrays in Python.