How to Create a Dataframe With Two Columns in Python
To create a dataframe with two columns in Python, you can use the DataFrame() function from the pandas library.
The following example shows how to create a dataframe with two columns in Python using the DataFrame() function.
Using DataFrame() Function
We can use the DataFrame() function to create a dataframe with two columns.
Suppose we have the following data:
# Import pandas library
import pandas as pd
# Create dataframe with two columns
office_stuff = pd.DataFrame({
'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
'Price': [4500, 550, 250, 50, 350, 50]
})
# Show dataframe
print(office_stuff)
Output: 👇️
Product_Name Price
0 Laptop 4500
1 Mobile 550
2 Printer 250
3 Keyboard 50
4 Scanner 350
5 Mouse 50
In this example, we use the DataFrame() function to create a dataframe office_stuff with two columns: Product_Name and Price.
The output shows the dataframe created with two columns.
Conclusion
We can use the DataFrame() function from the pandas library to create a dataframe with two columns in Python.