Summary of Attributes
| Attribute Name | Description |
|---|---|
| T | Returns the transposed version of the DataFrame. |
| at | Accesses a single value for a row/column label pair. |
| attrs | Returns a dictionary of global attributes for this dataset. |
| axes | Returns a list representing the axes of the DataFrame. |
| columns | Returns the column labels of the DataFrame. |
| dtypes | Returns the data types of each column in the DataFrame. |
| empty | Indicates whether the Series/DataFrame is empty. |
| flags | Returns attributes associated with this pandas object. |
| iat | Accesses a single value for a row/column pair by integer position. |
| iloc | (Deprecated) Purely integer-location based indexing for selection by position. |
| index | Returns the index (row labels) of the DataFrame. |
| loc | Accesses a group of rows and columns by label(s) or a boolean array. |
| ndim | Returns an int representing the number of dimensions/axes of the DataFrame. |
| shape | Returns a tuple representing the dimensionality of the DataFrame. |
| size | Returns an int representing the number of elements in this object. |
| style | Returns a Styler object. |
| values | Returns the Numpy representation of the DataFrame. |
Usage Examples
Creating the initial DataFrame:
import pandas as pd
import numpy as np
data = {
'height': np.random.randint(150, 200, size=10),
'weight': np.random.randint(40, 100, size=10),
'age': np.random.randint(18, 60, size=10),
'income': np.random.randint(2000, 8000, size=10)
}
df = pd.DataFrame(data)
# Original DataFrame:
height weight age income
0 179 88 37 6396
1 169 94 27 4011
2 187 64 25 3498
3 180 73 53 3699
4 172 69 49 4685
5 193 93 44 5724
6 189 66 24 3326
7 159 65 22 6547
8 178 62 40 5813
9 197 99 28 3545
1. T
print(df.T)
# Transposed DataFrame
0 1 2 3 4 5 6 7 8 9
height 179 169 187 180 172 193 189 159 178 197
weight 88 94 64 73 69 93 66 65 62 99
age 37 27 25 53 49 44 24 22 40 28
income 6396 4011 3498 3699 4685 5724 3326 6547 5813 3545
2. at, iat, loc, iloc
import pandas as pd
import numpy as np
data = {
'height': np.random.randint(150, 200, size=10),
'weight': np.random.randint(40, 100, size=10),
'age': np.random.randint(18, 60, size=10),
'income': np.random.randint(2000, 8000, size=10)
}
df = pd.DataFrame(data)
print(f'Original DataFrame:\n{df}')
print(f'Using at: {df.at[0, "height"]}')
print(f'Using iat: {df.iat[0, 0]}')
print(f'Using loc: {df.loc[0, "height"]}')
print(f'Using iloc: {df.iloc[0, 0]}')
print(f'Using loc to get multiple columns:\n{df.loc[:, "height"]}')
print(f'Using iloc to get multiple rows:\n{df.iloc[0, :]}')
df.at[0, 'height'] = 150
print(f'Updating using at: {df.at[0, "height"]}')
df.loc[0, 'height'] = 160
print(f'Updating using loc: {df.at[0, "height"]}')
# Output:
Original DataFrame:
height weight age income
0 169 66 36 3436
1 183 98 43 3654
2 197 60 48 5098
3 169 94 36 3356
4 196 94 20 3783
5 173 67 54 5072
6 157 95 40 4342
7 184 55 30 6855
8 186 56 59 5797
9 187 78 50 3057
Using at: 169
Using iat: 169
Using loc: 169
Using iloc: 169
Using loc to get multiple columns:
0 169
1 183
2 197
3 169
4 196
5 173
6 157
7 184
8 186
9 187
Name: height, dtype: int32
Using iloc to get multiple rows:
height 169
weight 66
age 36
income 3436
Name: 0, dtype: int32
Updating using at: 150
Updating using loc: 160
atandiatare used to access individual values, whereaslocandiloccan retrieve groups of rows or columns.atandlocuse label-based indexing, whileiatandilocuse integer-based indexnig.- All four methods allow modifying DataFrame contents.