Introduction
A DataFrame in pandas is a two-dimensional labeled data structure with columns of potentially different types. It's similar to a table in a database or a data frame in R. Understanding DataFrame attributes is crucial for effective data manipulation and analysis.
Common DataFrame Attributes
.shape
- Returns a tuple representing the dimensionality of the DataFrame.
- Example:
df.shape
.size
- Returns the number of elements in the DataFrame.
- Example:
df.size
.ndim
- Returns the number of dimensions of the DataFrame.
- Example:
df.ndim
.dtypes
- Returns the data types of each column.
- Example:
df.dtypes
.columns
- Returns the column labels of the DataFrame.
- Example:
df.columns
.index
- Returns the row labels of the DataFrame.
- Example:
df.index
.values
- Returns the underlying data of the DataFrame as a NumPy array.
- Example:
df.values
Examples
Let's consider the following DataFrame for examples:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [24, 27, 22],
'Salary': [70000, 80000, 65000]
}
df = pd.DataFrame(data)
.shape
print(df.shape) # Output: (3, 3)
.size
print(df.size) # Output: 9
.ndim
print(df.ndim) # Output: 2
.dtypes
print(df.dtypes) # Output: # Name object # Age int64 # Salary int64 # dtype: object
.columns
print(df.columns) # Output: Index(['Name', 'Age', 'Salary'], dtype='object')
.index
print(df.index) # Output: RangeIndex(start=0, stop=3, step=1)
.values
print(df.values) # Output: # [['Alice' 24 70000] # ['Bob' 27 80000] # ['Charlie' 22 65000]]
- import pandas as pddata = {'col1': [1, 2], 'col2': [3, 4]}df = pd.DataFrame(data)df_transpose = df.transpose()df_transpose_attr = df.Tprint("Dataframe:")print(df)print("Transpose using transpose() method:")print(df_transpose)print("Transpose using 'T'")print(df_transpose_attr)
Dataframe attributes list Attributes of dataframe Class 12 Pandas DataFrame attributes list Dataframe attributes pdf Dataframe attributes examples Pandas attributes and methods DataFrame' object has no attribute 'append Which attribute of data frame is used to retrieve its shape
0 Comments
Please do note create link post in comment section