How to Create A Dataframe With Two Variables in Python
To create a dataframe with two variables in Python, you can use the DataFrame() function from the pandas library.
Creating a dataframe with two variables is the same as creating a dataframe with two columns.
The following example shows how to create a dataframe with two variables in Python using the DataFrame() function.
Using DataFrame() Function
We can use the DataFrame() function to create a dataframe with two variables.
Suppose we have the following lists:
# Import pandas library
import pandas as pd
# Declare lists
date_list = ['01-03-2023', '01-03-2023', '01-03-2023']
product_code_list = ['A-101', 'A-102', 'A-103']
# Create dataframe
df = pd.DataFrame({
'Date': date_list,
'Product_Code': product_code_list
})
# Print dataframe
print(df)
Output: 👇️
Date Product_Code
0 01-03-2023 A-101
1 01-03-2023 A-102
2 01-03-2023 A-103
In this example, we use the DataFrame() function to create a dataframe df with two variables: Date and Product_Code.
The output shows the dataframe created with two variables.
Conclusion
We can use the DataFrame() function from the pandas library to create a dataframe with two variables in Python.