Introduction
When working with pandas, you may sometimes want to inspect one row of a DataFrame. By default, pandas displays a row horizontally, which can be difficult to read when the DataFrame has many columns.
Table of contents
- Introduction
- Create an example DataFrame
- Select one row from the DataFrame
- Display a pandas Series vertically
- Convert the row to a DataFrame
- Display one DataFrame row vertically using .T
- Display a specific row directly
- Use display() in a Jupyter Notebook
- Rename the value column
- Display a row with column names and values
- Print the row vertically as plain text
- Prevent pandas from truncating the output
- Complete reproducible example
- Summary
- References
A simple way to improve readability is to display the row vertically. This can be done using the transpose operator .T.
Create an example DataFrame
First, let’s create a simple pandas DataFrame:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import pandas as pd data = { "name": ["Alice", "Bob", "Charlie"], "age": [25, 30, 35], "city": ["New York", "Paris", "London"], "job": ["Data Analyst", "Developer", "Scientist"], "salary": [70000, 85000, 95000] } df = pd.DataFrame(data) df |
Output:
1 2 3 4 | name age city job salary 0 Alice 25 New York Data Analyst 70000 1 Bob 30 Paris Developer 85000 2 Charlie 35 London Scientist 95000 |
Select one row from the DataFrame
You can select one row using .iloc[].
For example, to select the first row:
1 2 3 | row = df.iloc[0] row |
Output:
1 2 3 4 5 6 | name Alice age 25 city New York job Data Analyst salary 70000 Name: 0, dtype: object |
In this case, row is a pandas Series.
You can check its type with:
1 | type(row) |
Output:
1 | pandas.core.series.Series |
Display a pandas Series vertically
A pandas Series is already displayed vertically in many cases.
For example:
1 | display(row) |
This is useful when you want to inspect the values of a single row in a readable format.
Convert the row to a DataFrame
If you want a cleaner table-style display, you can convert the Series to a DataFrame:
1 | pd.DataFrame(row) |
Output:
1 2 3 4 5 6 | 0 name Alice age 25 city New York job Data Analyst salary 70000 |
This displays the values vertically, with the original column names shown as the index.
Display one DataFrame row vertically using .T
Another common method is to first create a one-row DataFrame and then transpose it with .T.
1 | pd.DataFrame([row]).T |
Output:
1 2 3 4 5 6 | 0 name Alice age 25 city New York job Data Analyst salary 70000 |
The .T stands for transpose. It switches rows and columns.
This means that:
1 | pd.DataFrame([row]) |
creates a DataFrame with one horizontal row:
1 2 | name age city job salary 0 Alice 25 New York Data Analyst 70000 |
Then:
1 | pd.DataFrame([row]).T |
turns that row into a vertical display.
Display a specific row directly
You can also select and display a row vertically in one line:
1 | pd.DataFrame([df.iloc[0]]).T |
For the second row:
1 | pd.DataFrame([df.iloc[1]]).T |
For the third row:
1 | pd.DataFrame([df.iloc[2]]).T |
Use display() in a Jupyter Notebook
In a Jupyter Notebook, it is often better to use display() instead of print():
1 | display(pd.DataFrame([df.iloc[0]]).T) |
This gives a cleaner HTML table output in the notebook.
Rename the value column
By default, the transposed DataFrame may have a column named 0. You can rename it to something clearer:
1 2 3 4 | row_vertical = pd.DataFrame([df.iloc[0]]).T row_vertical.columns = ["value"] display(row_vertical) |
Output:
1 2 3 4 5 6 | value name Alice age 25 city New York job Data Analyst salary 70000 |
This is often easier to read.
Display a row with column names and values
Another clean approach is to reset the index:
1 2 3 4 | row_vertical = pd.DataFrame([df.iloc[0]]).T.reset_index() row_vertical.columns = ["column", "value"] display(row_vertical) |
Output:
1 2 3 4 5 6 | column value 0 name Alice 1 age 25 2 city New York 3 job Data Analyst 4 salary 70000 |
This format is very useful when you want a readable two-column table.
Print the row vertically as plain text
If you want to print the result as plain text, use .to_string():
1 | print(pd.DataFrame([df.iloc[0]]).T.to_string()) |
This is useful when working outside Jupyter Notebook or when debugging in a terminal.
Prevent pandas from truncating the output
If your row contains many columns or long text values, pandas may truncate the display.
You can force pandas to show all rows and full column values using:
1 2 3 4 5 6 | with pd.option_context( "display.max_rows", None, "display.max_columns", None, "display.max_colwidth", None ): display(pd.DataFrame([df.iloc[0]]).T) |
This is helpful when inspecting large DataFrames with many columns.
Complete reproducible example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import pandas as pd data = { "name": ["Alice", "Bob", "Charlie"], "age": [25, 30, 35], "city": ["New York", "Paris", "London"], "job": ["Data Analyst", "Developer", "Scientist"], "salary": [70000, 85000, 95000] } df = pd.DataFrame(data) row_vertical = pd.DataFrame([df.iloc[0]]).T row_vertical.columns = ["value"] display(row_vertical) |
Output:
1 2 3 4 5 6 | value name Alice age 25 city New York job Data Analyst salary 70000 |
Summary
To display a pandas row vertically, you can use:
1 | pd.DataFrame([df.iloc[0]]).T |
In a Jupyter Notebook, use:
1 | display(pd.DataFrame([df.iloc[0]]).T) |
For a cleaner output with a custom column name:
1 2 3 4 | row_vertical = pd.DataFrame([df.iloc[0]]).T row_vertical.columns = ["value"] display(row_vertical) |
Displaying a pandas row vertically is a simple way to improve readability, especially when your DataFrame contains many columns.
References
| Links | Site |
|---|---|
| pandas.DataFrame.T | pandas documentation |
| pandas.DataFrame.transpose | pandas documentation |
| pandas.Series | pandas documentation |
| pandas.DataFrame.iloc | pandas documentation |
| pandas.option_context | pandas documentation |
| Options and settings | pandas user guide |
| IPython.display | IPython documentation |
