Lesson 07
DataFrame Attributes
Read the shape of a table with shape, size, index, columns, axes and dtypes, then look inside with head, tail, info, and describe.
Before you analyse a table, find out what it is. How many rows and columns, what the columns are called, what type each one holds, and whether anything is missing. These five calls are the first thing to run on new data.
Size and labels
shape is (rows, columns), so shape[0] is the row count. size is every cell. index and columns hold the two sets of labels, and axes is both of them in a list.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
print(learners.shape, learners.size, learners.ndim)
print("rows:", learners.shape[0], "columns:", learners.shape[1])
print(learners.columns.tolist())
print(learners.index)Output:
(6, 5) 30 2
rows: 6 columns: 5
['name', 'track', 'city', 'hours', 'score']
RangeIndex(start=0, stop=6, step=1)Column types
dtypes gives one type per column. Text columns show as object. A numeric column that holds a missing value becomes float64, because NaN is a float.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
print(learners.dtypes)
print(learners.select_dtypes("number").columns.tolist())Output:
name object
track object
city object
hours int64
score int64
dtype: object
['hours', 'score']info
info() prints the row count, each column with its non-null count and type, and the memory used. The non-null counts are the quickest way to spot missing data.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
learners.info()Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 6 non-null object
1 track 6 non-null object
2 city 6 non-null object
3 hours 6 non-null int64
4 score 6 non-null int64
dtypes: int64(2), object(3)
memory usage: 372.0+ bytesdescribe
describe() summarizes the numeric columns. Pass include="all" to add the text columns, which report their distinct count and most common value instead of a mean.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
print(learners.describe())
print(learners.describe(include="object"))Output:
hours score
count 6.000000 6.000000
mean 39.500000 79.833333
std 11.113055 9.495613
min 24.000000 65.000000
25% 32.750000 75.250000
50% 40.000000 80.500000
75% 45.750000 86.500000
max 55.000000 91.000000
name track city
count 6 6 6
unique 6 3 3
top Avery Python Northgate
freq 1 2 2First and last rows
head() and tail() show five rows by default. On a real file of thousands of rows, this is how you look before you leap.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
print(learners.head(2))
print(learners.tail(2))Output:
name track city hours score
0 Avery Python Northgate 42 88
1 Jordan NumPy Eastport 31 74
name track city hours score
4 Morgan NumPy Eastport 47 82
5 Quinn pandas Westfield 38 79Key takeaways
-
shapeis(rows, columns)andsizeis every cell -
Text columns show as
objectindtypes -
info()shows non-null counts, so missing data stands out -
describe()covers numbers unless you include the rest -
Previous: Creating a DataFrame
Loading playground…
Check your understanding
DataFrame Attributes Quiz
What does df.shape return?
1 / 10