How to Create a Dataframe With 2 Rows in Python
To create a dataframe with 2 rows in Python, you can use lists of lists or a 2D numpy array.
The following examples show how to create a dataframe with 2 rows in Python.
Using Lists of Lists
We can use lists of lists to create a dataframe with 2 rows.
Suppose we have the following lists of lists:
import pandas as pd
# Declare list of lists
list_ = [["a", "b", "c"], ["A", "B", "C"]]
# Create dataframe
df = pd.DataFrame(list_)
# Show dataframe
print(df)
Output: 👇️
0 1 2
0 a b c
1 A B C
In this example, we use lists of lists to create a dataframe df with 2 rows. The output shows the dataframe created using lists of lists.
Using 2D Numpy Array
We can use a 2D numpy array to create a dataframe with 2 rows.
Suppose we have the following 2D numpy array:
import pandas as pd
import numpy as np
# Declare numpy array
array_ = np.array([["a", "b", "c"], ["A", "B", "C"]])
# Create dataframe
df = pd.DataFrame(array_)
# Show dataframe
print(df)
Output: 👇️
0 1 2
0 a b c
1 A B C
In this example, we use a 2D numpy array to create a dataframe df with 2 rows. The output shows the dataframe created using a 2D numpy array.
Conclusion
We can use lists of lists or a 2D numpy array to create a dataframe with 2 rows in Python.