天天看點

[轉載] 使用Python在Pandas Dataframe中建立索引

參考連結: Python中NumPy的基本切片Slicing和進階索引Indexing

In Numpy arrays, we are familiar with the concepts of indexing, slicing, and masking, etc. Similarly, Pandas to supports indexing in their Dataframe. If we are familiar with the indexing in Numpy arrays, the indexing in Pandas will be very easy.

      在Numpy數組中,我們熟悉索引,切片和遮罩等概念。類似地,Pandas支援在其Dataframe中建立索引。 如果我們熟悉Numpy數組中的索引編制,那麼Pandas中的索引編制将非常容易。  

     What is Indexing in Python?

      什麼是Python中的索引編制?  

     Selecting values from particular rows and columns in a dataframe is known as Indexing. By using Indexing, we can select all rows and some columns or some rows and all columns.

      從資料框中的特定行和列中選擇值稱為索引。 通過使用索引,我們可以選擇所有行和某些列,或者選擇某些行和所有列。  

     Let’s create a sample data in a series form for better understanding of indexing.

      讓我們以系列形式建立樣本資料,以更好地了解索引。  

     The output series looks like this,

      輸出系列看起來像這樣,  

     1    a3    b5    cdtype: object

     Now, here Python offers two types of indices

      現在,這裡Python提供了兩種類型的索引  

     Explicit Explicit  Implicit Implicit  

     Explicit Indexing:

      明确索引:  

     For the above dataset if we pass the command as,

      對于上述資料集,如果我們将指令傳遞為  

     ds[1] it uses explicit indices

      ds[1]它使用顯式索引  

     # If we pass the above command ds[1], the output will be'a'

     This is Explicit Indexing. Whereas, if we pass the command ds[1:3] it will use the implicit index style,

      這是顯式索引。 而如果我們傳遞指令ds[1:3] ,它将使用隐式索引樣式,  

     The output for the command ds[1:3] will be,

      指令ds[1:3]的輸出為  

     3    b5    cdtype: object

     These slicing and indexing can lead to some sort of confusion. To avoid this, Python offers some special indexer attributes:

      這些切片和索引編制可能導緻某種混亂。 為了避免這種情況,Python提供了一些特殊的indexer屬性:  

     loc 位置 

     The loc attribute allows indexing and slicing that always references the explicit index

      loc屬性允許始終引用顯式索引的索引和切片  

     iloc iloc 

     The iloc attribute allows indexing and slicing that always references the implicit index style

      iloc屬性允許始終引用隐式索引樣式的索引和切片  

     One common expression in Python code that everyone follows and practices is “explicit is better than implicit.”

      每個人都遵循并實踐的Python代碼中的一個常見表達是“明确勝于隐含”。  

     Let’s take a sample dataset and see how indexing can be performed in different formats.

      讓我們以一個樣本資料集為例,看看如何以不同的格式執行索引。  

     We are using the data of NBA players from kaggle.

      我們正在使用kaggle提供的NBA球員資料。  

     The Dataset looks like this,

      資料集看起來像這樣,  

        NBA Players sample dataset

         NBA球員樣本資料集 

      單列 (Single Column) 

     To display a single column from the dataframe, we will mention the column name in the print statement.

      為了顯示資料框中的單個列,我們将在print語句中提及列名。  

     The output will look like this,

      輸出将如下所示:  

       Single Column from Dataset

        資料集中的單列 

      多列 (Multiple Columns) 

     Let’s try to display the ‘Age’, ‘College’ and ‘Draft Year’ of the players. We can display multiple columns in the following way,

      讓我們嘗試顯示球員的“年齡”,“大學”和“起草年”。 我們可以通過以下方式顯示多列,  

     The multiple columns will display like this,

      多列将顯示如下,  

       Multiple Columns

        多列 

      .loc方法 (.loc Method) 

     Indexing using .loc method. If we use the .loc method, we have to pass the data using its Label name.

      使用.loc方法建立索引。 如果使用.loc方法,則必須使用其Label名稱傳遞資料。  

      單排 (Single Row) 

     To display a single row from the dataframe, we will mention the row’s index name in the .loc method.

      為了顯示資料框中的一行,我們将在.loc方法中提及該行的索引名稱。  

     The whole row information will display like this,

      整行資訊将像這樣顯示,  

       Single Row information

        單行資訊 

      多行 (Multiple Rows) 

     Same as single row, pass the rows information in the print command to display the information.

      與單行相同,在print指令中傳遞行資訊以顯示資訊。  

     The output will be,

      輸出将是  

       Multiple Rows

        多行 

      選擇行和列 (Selecting Rows and Columns) 

     We can also select multiple rows and multiple columns at a time using the .loc method.

      我們還可以使用.loc方法一次選擇多個行和多個列。  

     The output will be like this,

      輸出将是這樣,  

       Displaying Rows and Columns

        顯示行和列 

      所有行和某些列 (All Rows and Some Columns) 

     To display all the rows with some columns using the .loc method.

      使用.loc方法顯示帶有某些列的所有行。  

     The output of the above code will be like this,

      上面代碼的輸出将是這樣,  

     The same output can be achieved by simply giving column names without using the .loc method as shown in Selecting Multiple Columns.

      隻需提供列名而無需使用 選擇多列中所示的 .loc 方法 ,就可以實作相同的輸出。  

     The output will be same as the one above,

      輸出将與上面的輸出相同,  

       Same output without .loc

        沒有.loc的相同輸出 

      .iloc方法 (.iloc Method) 

     Indexing using the .iloc method. If we use the .iloc method, we have to pass the data using its Position. It is very similar to the .loc method, the only difference is .iloc uses integers to extract the information.

      使用.iloc方法建立索引。 如果使用.iloc方法,則必須使用其Position傳遞資料。 它與.loc方法非常相似,唯一的差別是.iloc使用整數來提取資訊。  

      單排 (Single Row) 

     We have to pass a single integer in the .iloc method to get the row information.

      我們必須在.iloc方法中傳遞一個整數以擷取行資訊。  

     The output will be,

      輸出将是  

       Single Row using .iloc method

        單行使用.iloc方法 

      多行 (Multiple Rows) 

     To select multiple rows, we have to pass the positions of the selected rows.

      要選擇多行,我們必須傳遞所選行的位置。  

     The output will look something like this,

      輸出看起來像這樣,  

      選擇行和列 (Selecting Rows and Columns) 

     To display a specific number of rows and columns, we create a list of integer for rows and a list of integer for columns and pass to iloc function.

      要顯示特定數量的行和列,我們為行建立一個整數清單,為列建立一個整數清單,然後傳遞給iloc函數。  

     The output will be,

      輸出将是  

       Rows and Columns iloc method

        行和列iloc方法 

      所有行和某些列 (All Rows and Some Columns) 

     To display all the rows, we have to pass “:” and the integers for columns.

      要顯示所有行,我們必須傳遞“:”和列的整數。  

     The output will look like something like this,

      輸出看起來像這樣,  

       All Rows and Some columns iloc

        所有行和某些列iloc 

     If the columns in the dataset are of “int” or “float” type, then we can apply all the numeric operations to the column directly and manipulate the data to our requirements.

      如果資料集中的列為“ int”或“ float”類型,則我們可以将所有數值運算直接應用于該列,并根據需要操作資料。  

      使用.loc方法的數值運算 (Numeric Operations using .loc method) 

     The output will be like this,

      輸出将是這樣,  

       Numeric operations using loc function

        使用loc函數進行數值運算 

      使用.iloc方法的數值運算 (Numeric operations using .iloc Method) 

     The output will be same as the previous one with .loc method

      輸出将與使用.loc方法的上一個輸出相同  

      結論 (Conclusion) 

     We can conclude this article in three simple statements.

      我們可以用三個簡單的語句來結束本文。  

     To avoid confusion on Explicit Indices and Implicit Indices we use .loc and .iloc methods. 為避免對顯式索引和隐式索引造成混淆,我們使用.loc和.iloc方法。 .loc method is used for label based indexing. .loc方法用于基于标簽的索引。 .iloc method is used for position based indexing. .iloc方法用于基于位置的索引。 

     These are the three main statements, we need to be aware of while using indexing methods for a Pandas Dataframe in Python.

      這是三個主要語句,我們在使用Python中的Pandas Dataframe的索引方法時需要注意。  

     Thank you for reading and Happy Coding!!!

      感謝您的閱讀和快樂編碼!!!  

      在這裡檢視我以前關于Python的文章 (Check out my previous articles about Python here) 

     Seaborn: Python Seaborn:Python Pandas: Python 熊貓:Python Matplotlib: Python Matplotlib:Python NumPy: Python NumPy:Python Data Visualization and its Importance: Python 資料可視化及其重要性:Python Time Complexity and Its Importance in Python 時間複雜度及其在Python中的重要性 Python Recursion or Recursive Function in Python Python中的Python遞歸或遞歸函數 

  翻譯自: https://towardsdatascience.com/indexing-in-pandas-dataframe-using-python-63dcc6242323