Page 223 - AI_Ver_3.0_class_11
P. 223
'Rating': [4.5, 4.0, 4.7, 4.3]
}
df = pd.DataFrame(data)
# Display the DataFrame
print(df)
Output:
Product Price Stock Rating
0 Laptop 1000 50 4.5
1 Tablet 500 150 4.0
2 Smartphone 800 200 4.7
3 Monitor 300 100 4.3
Some common attributes of DataFrame are as follows:
• • columns: This attribute returns an Index object containing the column labels of the DataFrame.
print(df.columns)
Output: Index(['Product', 'Price', 'Stock', 'Rating'], dtype='object')
• • index: This attribute returns the row labels of the DataFrame.
print(df.index)
Output: RangeIndex(start=0, stop=4, step=1)
• • dtypes: This attribute returns the data types of each column in the DataFrame.
print(df.dtypes)
Output:
Product object
Price int64
Stock int64
Rating float64
dtype: object
• • shape: This attribute returns a tuple representing the dimensions of the DataFrame, i.e., (number of rows, number
of columns).
print(df.shape)
Output: (4, 4)
• • size: This attribute returns the total number of elements in the DataFrame.
print(df.size)
Output: 16
• • values: This attribute returns a two-dimensional NumPy array representing the underlying data of the DataFrame.
print(df.values)
Output:
[['Laptop' 1000 50 4.5]
['Tablet' 500 150 4.0]
['Smartphone' 800 200 4.7]
['Monitor' 300 100 4.3]]
Python Programming 221

