Index is an important tool for Pandas. Through indexes, you can select specific rows and columns from DataFame. This way of selecting data is called "subset selection".
In Pandas, the index value is also called a label, which is displayed in bold in Jupyter notebooks. Indexing can speed up data access, it is like a bookmark of data, through which it can enable quick search of data.
Create an index
The index index is further explained through examples. Below is a data with the index index and use read_csv() to read the data:
import pandas as pd data = pd.read_csv("") print(data)
Output result:
ID Name Age City Salary
0 1 Jack 28 Beijing 22000
1 2 Lida 32 Shanghai 19000
2 3 John 43 Shenzhen 12000
3 4 Helen 38 Hengshui 3500
Read multiple columns of data through column index (label).
import pandas as pd #Set "Name" as row indexdata = pd.read_csv("", index_col ="Name") # Select multiple columns of data through column labelsa = data[["City","Salary"]] print(a)
Output result:
City Salary
Name
Jack Beijing 22000
Lida Shanghai 19000
John Shenzhen 12000
Helen Hengshui 3500
Let's take a look at another set of simple examples:
import pandas as pd info =pd.read_csv("", index_col ="Name") #Get single column data, or pass in ["Salary"] as a lista =info["Salary"] print(a)
Output result:
Salary
Name
Jack 22000
Lida 19000
John 12000
Helen 3500
Setting the index
set_index() Sets the existing column label to the DataFrame row index. In addition to adding indexes, you can also replace existing indexes. For example, you can also set Series or a DataFrme to the index of another DataFrame. Examples are as follows:
info = ({'Name': ['Parker', 'Terry', 'Smith', 'William'], 'Year': [2011, 2009, 2014, 2010], 'Leaves': [10, 15, 9, 4]}) #Set Name as row indexprint(info.set_index('Name'))
Output result:
Year Leaves
Name
Parker 2011 10
Terry 2009 15
Smith 2014 9
William 2010 4
Reset index
You can use reset_index() to restore the initial row index, as shown below:
import pandas as pd import numpy as np info = ([('William', 'C'), ('Smith', 'Java'), ('Parker', 'Python'), ('Phill', )], index=[1, 2, 3, 4], columns=('name', 'Language')) print(info) print(info.reset_index())
Output result:
Before reset:
name Language
1 William C
2 Smith Java
3 Parker Python
4 Phill NaN
After reset:
index name Language
0 1 William C
1 2 Smith Java
2 3 Parker Python
3 4 Phill NaN
This is the end of this article about the implementation of Pandas index operation index. For more related Pandas index operation index content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!