Pandas DataFrame
last modified November 17, 2026 Python 3.15
A DataFrame is a two-dimensional, table-like data structure in Pandas. It has rows and columns, each column has a name, and each row has a label held in the index. You can think of it as a spreadsheet or an SQL table that lives in Python. DataFrames are built on top of Series: every column of a DataFrame is a Pandas Series.
This article is about the Pandas table type. The Python dataframe article is a separate text about a plain-Python data structure with a similar name. The examples below were verified with Pandas 3.1.1.
Constructing a DataFrame
The pd.DataFrame constructor accepts many kinds of input. Two
common ones are a dictionary mapping column names to lists of values, and a
list of dictionaries where each dictionary becomes one row. Missing keys in
the list-of-dicts form are filled with NaN.
import pandas as pd
df1 = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [30, 25, 35],
'City': ['Prague', 'Bratislava', 'Vienna']
})
print('from dict of lists:')
print(df1)
print()
df2 = pd.DataFrame([
{'Name': 'Alice', 'Age': 30},
{'Name': 'Bob', 'Age': 25},
{'Name': 'Charlie', 'Age': 35}
])
print('from list of dicts:')
print(df2)
In both cases Pandas creates a numerical row index starting at zero, shown in the leftmost column of the output. The dictionary form is convenient when you already have one list per column; the list-of-dicts form mirrors JSON-like data and is what you typically get from an API response. The reading and writing CSV article shows how to build a DataFrame from a file instead.
$ python df_construct.py
from dict of lists:
Name Age City
0 Alice 30 Prague
1 Bob 25 Bratislava
2 Charlie 35 Vienna
from list of dicts:
Name Age
0 Alice 30
1 Bob 25
2 Charlie 35
Inspecting a DataFrame
Before working with a table it is useful to check its size and the types of
its columns. The shape attribute returns a tuple of
(rows, columns), dtypes lists each column's data
type, and info() prints a compact summary. The
head() and tail() methods show the first and last
rows; both accept the number of rows to display.
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [30, 25, 35],
'Score': [88.5, 92.0, 79.5]
})
print('shape:', df.shape)
print()
print('dtypes:')
print(df.dtypes)
print()
print('head(2):')
print(df.head(2))
print()
print('tail(1):')
print(df.tail(1))
print()
print('info():')
df.info()
The shape (3, 3) means three rows and three columns. The
dtypes output shows that Name holds strings,
Age holds integers, and Score holds floating-point
numbers. The final line of dtypes says dtype: object
because the collection of column types is itself stored in an object Series.
The info() summary also reports memory usage; its exact value
can differ between Pandas versions. Note that info() writes
directly to the terminal, so it is not followed by print().
$ python df_inspect.py
shape: (3, 3)
dtypes:
Name str
Age int64
Score float64
dtype: object
head(2):
Name Age Score
0 Alice 30 88.5
1 Bob 25 92.0
tail(1):
Name Age Score
2 Charlie 35 79.5
info():
<class 'pandas.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null str
1 Age 3 non-null int64
2 Score 3 non-null float64
dtypes: float64(1), int64(1), str(1)
memory usage: 204.0 bytes
Selecting columns and rows
A single column is selected with df['column'], which returns a
Series. Rows are selected by label with .loc and by position
with .iloc; both return a Series representing one row. The
loc function article covers label-based
selection in much greater depth.
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [30, 25, 35],
}, index=['a', 'b', 'c'])
print('single column:')
print(df['Name'])
print()
print('row by label (loc):')
print(df.loc['b'])
print()
print('row by position (iloc):')
print(df.iloc[0])
The expression df['Name'] returns the whole
Name column as a Series, keeping the labels
a, b, and c. With
df.loc['b'] we ask for the row whose index label is
'b'; with df.iloc[0] we ask for the first row by
position. In each single-row result the column names become the index and
the original row label becomes the Series name, as shown by
Name: b and Name: a.
$ python df_select.py single column: a Alice b Bob c Charlie Name: Name, dtype: str row by label (loc): Name Bob Age 25 Name: b, dtype: object row by position (iloc): Name Alice Age 30 Name: a, dtype: object
Adding and deleting columns
Columns are created or replaced by assigning to a new or existing column
name with []. The del keyword removes a column.
Assigning a scalar sets the same value in every row; assigning a list must
provide one value per row.
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [30, 25]})
df['City'] = ['Prague', 'Bratislava']
print('after adding City:')
print(df)
print()
df['Age'] = df['Age'] + 1
print('after updating Age:')
print(df)
print()
del df['City']
print('after deleting City:')
print(df)
Assigning df['City'] adds a third column, with its values
matched to rows in order. The line df['Age'] = df['Age'] + 1
shows that column arithmetic works element by element, incrementing every
age. Finally del df['City'] removes that column and leaves the
modified Age values in place. All three operations change the
DataFrame in place.
$ python df_columns.py
after adding City:
Name Age City
0 Alice 30 Prague
1 Bob 25 Bratislava
after updating Age:
Name Age City
0 Alice 31 Prague
1 Bob 26 Bratislava
after deleting City:
Name Age
0 Alice 31
1 Bob 26
The columns and index attributes
The columns and index attributes expose the
column names and row labels. Both are Index objects, but they can be
replaced by assigning a new list of the same length, which is a convenient
way to rename every column or row at once.
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [30, 25]}, index=['r1', 'r2'])
print('columns:', list(df.columns))
print('index:', list(df.index))
print()
df.columns = ['first_name', 'years']
df.index = ['x', 'y']
print('after renaming:')
print(df)
We first convert the Index objects to Python lists to print them. Then we
assign new lists to df.columns and df.index; the
data stays in place while the labels change. For renaming only some labels,
the rename method is usually a better tool. The labels are what
label-based selection such as .loc looks up.
$ python df_attrs.py columns: ['Name', 'Age'] index: ['r1', 'r2'] after renaming: first_name years x Alice 30 y Bob 25
Transposing a DataFrame
The T attribute, short for the transpose method,
swaps rows and columns: the former column names become row labels and the
former row labels become column names. It is handy for changing the
orientation of a small table for display.
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=['r1', 'r2', 'r3'])
print('original:')
print(df)
print()
print('transposed:')
print(df.T)
The original table has three rows (r1 to r3) and
two columns (A and B). After transposing, the two
columns become rows and the three row labels become columns, giving a
two-row by three-column table. Note that transposing can change the data
types of the resulting columns because a column now mixes values that were
previously in different columns.
$ python df_transpose.py
original:
A B
r1 1 4
r2 2 5
r3 3 6
transposed:
r1 r2 r3
A 1 2 3
B 4 5 6
Source
Pandas documentation - DataFrame
Pandas user guide - 10 minutes to pandas
Pandas documentation - DataFrame.loc
Pandas documentation - DataFrame.T
In this article we have built DataFrames from dictionaries and lists of
dictionaries, inspected them with shape, dtypes,
info(), head(), and tail(), selected
columns and rows, added and removed columns, renamed labels through the
columns and index attributes, and transposed a
table. See also the Pandas Series,
loc function, and
reading and writing CSV articles.
Author
List all Pandas tutorials.