labeled data structure. So, each row and each column can be labeled. If we do not provide any labels, the constructor pandas.DataFrame() uses the numbers 0, 1, 2, etc. as labels.
While creating df2 DataFrame, we are using the same list, but we are also providing labels for rows or indexes and labels for columns.
df2 = pandas.DataFrame(list1, index=["Row 1", "Row 2", "Row 3"], columns=["Column 1", "Column 2", "Column 3"]) print(df2)
So, when we print df2, it gives the following output:
Column 1 Column 2 Column 3 Row 1 1 2 3 Row 2 4 5 6 Row 3 7 8 9
Similarly, we can also use a dictionary to create a pandas DataFrame. Here, the DataFrame df3 is created using the dictionary dict1.
dict1 = {“Column 1”: [1, 2, 3], "Column 2": [4, 5, 6], "Column 3": [7, 8, 9]}
df3 = pandas.DataFrame(dict1)
print(df3)
So, when we print df3, it prints the following:
Column 1 Column 2 Column 3 0 1 4 7 1 2 5 8 2 3 6 9
So, the complete output of the given program is like the following:
0 1 2
0 1 2 3
1 4 5 6
2 7 8 9
Column 1 Column 2 Column 3
Row 1 1 2 3
Row 2 4 5 6
Row 3 7 8 9
Column 1 Column 2 Column 3
0 1 4 7
1 2 5 8
2 3 6 9








































0 Comments