data = {'A': [1, 2], 'B': [3, 4]}
df = pd.DataFrame(data)
print(df.shape)
Explanation:
data = {'A': [1, 2], 'B': [3, 4]}
- A dictionary named data is created with two key-value pairs:
- Key 'A' maps to the list [1, 2]
- Key 'B' maps to the list [3, 4]
- This dictionary represents column data for a table:
- df = pd.DataFrame(data)
- A Pandas DataFrame is created from the dictionary.
- Each key in the dictionary ('A' and 'B') becomes a column in the DataFrame.
- The values in the lists ([1, 2] for 'A' and [3, 4] for 'B') become the rows under the respective columns.
- The resulting DataFrame looks like this:
- print(df.shape)
- The shape attribute of a DataFrame returns a tuple representing its dimensions:(number_of_rows, number_of_columns)
- For this DataFrame:
- Number of rows = 2 (index 0 and 1)
- Number of columns = 2 ('A' and 'B')
- df.shape returns (2, 2).
Final Output:
This tells us the DataFrame has 2 rows and 2 columns.
0 Comments:
Post a Comment