Got a DataFrame and want a quick look at what’s inside? These four methods show you the key info in one line. Here’s an 8-row example to work with:
import pandas as pd
df = pd.DataFrame({
"name": ["Alice", "Bob", "Carol", "Dave", "Eve", "Frank", "Grace", "Heidi"],
"dept": ["IT", "IT", "HR", "HR", "IT", "HR", "IT", "HR"],
"salary": [5000, 6000, 5500, 5200, 7000, 5800, 6200, 5400],
})Example 1: head() — first few rows #
df.head()output:
name dept salary 0 Alice IT 5000 1 Bob IT 6000 2 Carol HR 5500 3 Dave HR 5200 4 Eve IT 7000
Example 2: tail() — last few rows #
df.tail()output:
name dept salary 3 Dave HR 5200 4 Eve IT 7000 5 Frank HR 5800 6 Grace IT 6200 7 Heidi HR 5400
Example 3: info() — column types #
df.info()output:
<class 'pandas.DataFrame'> RangeIndex: 8 entries, 0 to 7 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 name 8 non-null str 1 dept 8 non-null str 2 salary 8 non-null int64 dtypes: int64(1), str(2) memory usage: 324.0 bytes
Example 4: describe() — numeric summary #
df.describe()output:
salary count 8.000000 mean 5762.500000 std 641.287767 min 5000.000000 25% 5350.000000 50% 5650.000000 75% 6050.000000 max 7000.000000
