• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Pandas 101 / How to convert row names to a column in Pandas

How to convert row names to a column in Pandas

August 4, 2024 by cmdlinetips

In this tutoriual, we will learn how to convert the row name or row index name in a Pandas dataframe to a column. We will use Pandas’ reset_index() function to convert the index with name to a column.

Let us load Pandas.

import pandas as pd
pd.<strong>version</strong>
&#039;2.1.4&#039;

Let us create a small dataframe in Pandas with rown index names.

stock_price = [426, 422, 418,417, 408]
symbol = ["MSFT","MSFT","MSFT","MSFT","MSFT"]
date = ['2024-07-29', '2024-07-30',
        '2024-07-31', '2024-08-01',
        '2024-08-02']

Our toy data frame has date values as row index names.

df = pd.DataFrame({&#039;symbol&#039;:symbol,
                   &#039;price&#039;: stock_price},
                  index=date)
df

<pre><code>symbol  price
</code></pre>

2024-07-29  MSFT    426
2024-07-30  MSFT    422
2024-07-31  MSFT    418
2024-08-01  MSFT    417
2024-08-02  MSFT    408

And the date is a a key variable of the dataframe and it is better be one of the columns instead of row index names. To conver the row index name we will use two steps.

In the first step, we will specify a name to the row index name.

df.index.name = "date"

We can see that now the dataframe looks like this with the name for the row index.

    symbol  price
date<br />
2024-07-29  MSFT    426
2024-07-30  MSFT    422
2024-07-31  MSFT    418
2024-08-01  MSFT    417
2024-08-02  MSFT    408

It is multi-index dataframe, we can simply it with Pandas reset_index() function.

df.reset_index()

date    symbol  price
0   2024-07-29  MSFT    426
1   2024-07-30  MSFT    422
2   2024-07-31  MSFT    418
3   2024-08-01  MSFT    417
4   2024-08-02  MSFT    408

Another way to convert row index as a separate column in the dataframe us to use Pandas’ rename_axis() function with Pandas reset_index() function. An advantage of this approach is that we can chain these two functions into a single statement as shown below.

(df
 .rename_axis("Date")
 .reset_index()
)

And we get the result we need.

Date    symbol  price
0   2024-07-29  MSFT    426
1   2024-07-30  MSFT    422
2   2024-07-31  MSFT    418
3   2024-08-01  MSFT    417
4   2024-08-02  MSFT    408

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Convert a Column to Row Name/Index in Pandas? Change Column Names and Row Indexes in PandasHow To Change Column Names and Row Indexes in Pandas? Default ThumbnailHow To Change Pandas Column Names to Lower Case Default ThumbnailHow to Convert a Column to Datetime type with Pandas

Filed Under: Pandas 101, Pandas rename_axis, Pandas reset_index, Python Tips Tagged With: Convert row names to a column in Pandas

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version