What will the following code output?
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df.shape)
A) (2, 2)
B) (2, 1)
C) (1, 2)
D) [2, 2]
Step-by-Step Breakdown:
Importing Pandas:
import pandas as pd imports the pandas library, which provides tools for working with structured data.Creating a Dictionary:
- The dictionary data has two keys: 'Name' and 'Age'.
- 'Name' corresponds to a list of strings: ['Alice', 'Bob'].
- 'Age' corresponds to a list of integers: [25, 30].
Creating a DataFrame:
- The pd.DataFrame() function converts the dictionary data into a DataFrame:
- Each key becomes a column name.
- Each list becomes the values of the column.
- The DataFrame has 2 rows (index 0 and 1) and 2 columns (Name and Age).
Printing the Shape:
- df.shape returns a tuple (rows, columns).
- Here:
- Rows = 2 (Alice and Bob)
- Columns = 2 (Name and Age)
Final Output:
Why this happens:
- shape attribute provides a quick way to check the dimensions of the DataFrame.
- The first value 2 refers to the number of rows.
- The second value 2 refers to the number of columns.
0 Comments:
Post a Comment