In this Python tutorial, we will learn how to set column as Index in Python Pandas. Also, we will cover these topics.
- Set Column as Index Pandas DataFrame
- Set First Column as Index Pandas
- Set Date Column as Index Pandas
- Set Datetime column as Index Pandas
- Set Column names as Index Pandas
- Set Column as Row Index Pandas
- Set Two Column as Index Pandas
If you are new to Pandas, check out Pandas in Python.
For demonstration purposes, we are using Steam Player Data that we have downloaded from Kaggle.
Set Column as Index Pandas DataFrame
In this section, we will learn how to set column(s) as Index in Pandas DataFrame.
- Python Pandas provide wide varieties of options to process data.
- Out of these options, one option is dataframe.set_index().
- Using dataframe.set_index() methon, we can set any column as a Index.
- This method accepts name(s) of columns that you want to set as Index.
- In our example on jupyter notebook, we have set date as a index value.
Read How to use Pandas drop() function in Python
Set First Column as Index Pandas
In this section, we will learn how to set the First Column as Index Pandas.
- Using dataframe.set_index() method in Python Pandas we can set any column as an Index.
- In the dataset we are using, Month_Year is the first column. So here is the following code to set the first column as Index in Pandas.
df.set_index('Month_Year')
- Here is the implementation of an example on Jupyter Notebook.
Set Date Column as Index Pandas
In this section, we will learn how to set the date column as an Index in Pandas.
- In our dataset there is a date column, and we will learn how to set date column as Index in Python Pandas.
- Using dataframe.set_index() method in Python Pandas and by passing Date column inside this method we can set the date as a Index column.
- Here is the implementation on Jupyter Notebook.
Check out, Crosstab in Python Pandas
Set Datetime column as Index Pandas
In this section, we will learn how to set the DateTime column as an Index in Pandas.
- Setting Datetime column as Index in Pandas is no different than other we did in this tutorial.
- Since we don’t have datetime column so we will demonstrate it using date column.
- Using Python Pandas dataframe.set_index() method we can set datetime column as Index for the dataframe.
- Here is the implementation on Jupyter Notebook.
Read Missing Data in Pandas in Python
Set Column names as Index Pandas
In this section, we will learn how to set column names as Index in Python Pandas.
- Using dataframe.set_index() method we can set any column to Index in Python Pandas.
- To set multiple column names we can pass the list of column names inside the dataframe.set_index() method.
- This will set multiple column names as Index in Python Pandas.
df.set_index(['Month_Year', 'Date'])
- Here is the implementation on Jupyter Notebook.
Set Column as Row Index Pandas
In this section, we will learn how to set a column as a row index in Pandas.
- Using dataframe.set_index() method in Python Pandas we can set column as Row Index in Pandas.
- All you have to do is pass the name of the column inside this method. You can also pass list of columns inside thos method.
- In our example, we have set Peak_Players column as Row index in Python Pandas.
- Here is the implementation on Jupyter Notebook.
Set Two Column as Index Pandas
In this section, we will learn how to set two-column as Indexes in Pandas.
- Using dataframe.set_index() method we can set any column to Index in Python Pandas.
- To set multiple column names we can pass the list of column names inside the dataframe.set_index() method.
- This will set multiple column names as Index in Python Pandas.
df.set_index(['Month_Year', 'Date'])
- Here is the implementation on Jupyter Notebook.
You may like the following Python Pandas tutorials:
- Python Pandas Write DataFrame to Excel
- How to Subset a DataFrame in Python
- How to Find Duplicates in Python DataFrame
- Count Rows in Pandas DataFrame
- Python Pandas DataFrame Iterrows
- Pandas replace multiple values
- Check If DataFrame is Empty in Python Pandas
In this tutorial, we have learned how to set columns as Index in Python Pandas. Also, we have covered these topics.
- Set Column as Index Pandas DataFrame
- Set First Column as Index Pandas
- Set Date Column as Index Pandas
- Set Datetime column as Index Pandas
- Set Column names as Index PAndas
- Set Column as Row Index Pandas
- Set Two Column as Index Pandas
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.