如何在Pandas資料框中將列名轉換為小寫?


本文將介紹如何在Pandas資料框中將列名轉換為小寫。透過三個不同的例子,講解了將資料框列轉換為小寫的方法。這些例子都使用了Kaggle上提供的Zomato資料集。Kaggle資料集以CSV(逗號分隔值)格式提供,因此首先下載該檔案,然後使用pandas將其轉換為資料框。

在第一個例子中,Python程式使用`str.lower()`函式將列值轉換為小寫。在第二個例子中,使用`map(str.lower)`函式將資料框列轉換為小寫。第三個例子分為兩部分。首先,介紹了使用`apply(lambda x: x.lower())`函式將列內容轉換為小寫的方法,然後介紹了使用`map(str.lower, dataframe.columns)`函式將列標題轉換為小寫的方法。

儲存資料分析所需的資料檔案/csv檔案

在這些例子中,我們將使用Kaggle上提供的資料。登入Kaggle並從此連結下載csv檔案https://www.kaggle.com/datasets/shrutimehta/zomato-restaurants-data

資料集以CSV檔案形式提供。

使用的Zomato.CSV檔案

圖:此csv檔案包含9551行和21列。

示例1:使用`str.lower()`函式在資料框列上將內容轉換為小寫

設計步驟和程式碼

  • 步驟1 − 首先匯入pandas。現在讀取zomato.csv檔案,因為這裡給出的資料集將用於將其載入到資料框中。

  • 步驟2 − 建立一個名為dff1的資料框,並使用pandas中的`read_table`函式讀取CSV檔案。現在類似地建立另一個名為dff2的資料框,但是使用餐廳名稱作為索引列。

  • 步驟3 − 為這些使用`delimiter=','`和zomato.csv的路徑。使用`head`函式列印此資料框中的一些行和列。

  • 步驟4 − 從dff2中選擇一些需要轉換為小寫的列。這個新的資料框是dff3。在dff3的一列上應用`str.lower()`,並將其轉換為小寫。

  • 步驟5 − 執行程式並檢查結果。

在python檔案中編寫以下程式碼

import pandas as pdd
dff1 = pdd.read_table("C:/Users/saba2/Desktop/article/articles_py/tsv/zomato.csv",delimiter=',', encoding='utf-8')
print("\n The complete dataset: ")
print(dff1.head())

dff2 = pdd.read_table('C:/Users/saba2/Desktop/article/articles_py/tsv/zomato.csv', delimiter=',',encoding='utf-8',index_col=1)
#print(" \nThe complete dataset with specified index : ")
#print(dff2.head())
dff3=dff2[["Rating color", "Rating text"]]
print("\nPrinting Shape for Rating color and Rating text of Restaurants : ")
print(dff3.shape)
print("\nPrinting Rating color and Rating text of Restaurants : ")
print(dff3.head())

print("\nConverting Rating color Column in lowercase : ")
print(dff3['Rating color'].str.lower())

輸出

在命令視窗中執行python檔案

圖1:使用cmd視窗顯示結果。

示例2:使用`map(str.lower)`函式在資料框列上將內容轉換為小寫

設計步驟和程式碼

  • 步驟1 − 首先匯入pandas。現在讀取zomato.csv檔案,因為這裡給出的資料集將用於將其載入到資料框中。

  • 步驟2 − 建立一個名為dff2的資料框,並使用pandas中的`read_csv`函式讀取CSV檔案。只需使用`usecols`選擇兩列,“餐廳名稱”和“評分文字”。

  • 步驟3 − 為這些使用`delimiter=','`和zomato.csv的路徑。使用`head`函式列印此資料框中的一些行和列。

  • 步驟4 − 在dff2的一列上應用`map(str.lower)`,並將其轉換為小寫。

  • 步驟5 − 執行程式並檢查結果。

在python檔案中編寫以下程式碼

import pandas as pdd

dff2 = pdd.read_csv('C:/Users/saba2/Desktop/article/articles_py/tsv/zomato.csv', sep=',',usecols=['Restaurant Name', 'Rating text'])

print("\nPrinting Restaurant Name and Rating text of Restaurants : ")
print(dff2.head())

print("\nPrinting Shape of the dataframe : ")
print(dff2.shape)

print("\nConverting Rating text Column in lowercase : ")
dff2["Lowercase Rating text"]= dff2['Rating text'].map(str.lower)

print("\nPrinting Shape of the dataframe after adding the Lowercase Rating text column : ")
print(dff2.shape)
print("\nPrinting the new added column with lowercase values : ")
print(dff2.head())

輸出

在命令視窗中執行python檔案

圖2:使用cmd視窗顯示結果。

示例3:將列標題和內容更改為小寫

設計步驟和程式碼

  • 步驟1 − 首先匯入pandas。現在讀取zomato.csv檔案,因為這裡給出的資料集將用於將其載入到資料框中。

  • 步驟2 − 建立一個名為dff2的資料框,並使用pandas中的`read_csv`函式讀取CSV檔案。使用`usecols`選擇三列,“餐廳名稱”、“評分文字”和“評分顏色”。

  • 步驟3 − 為這些使用`delimiter=','`和zomato.csv的路徑。使用`head`函式列印此資料框中的一些行和列。

  • 步驟4 − 在dff2的“評分文字”上使用`apply(lambda x: x.lower())`將其轉換為小寫。將這個小寫列新增到資料框。

  • 步驟5 − 現在對“評分顏色”列使用步驟4。

  • 步驟6 − 在資料框列上應用`map(str.lower, dataframe.columns)`函式,將列標題轉換為小寫。

  • 步驟7 − 執行程式並檢查結果。

在python檔案中編寫以下程式碼

import pandas as pdd

dff2 = pdd.read_csv('C:/Users/saba2/Desktop/article/articles_py/tsv/zomato.csv', sep=',',usecols=['Restaurant Name', 'Rating color', 'Rating text'])

print("\nPrinting Restaurant Name, Rating Color and Rating text of Restaurants : ")
print(dff2.head())

print("\nPrinting Shape of the dataframe : ")
print(dff2.shape)

print("\nConverting Rating text Column in lowercase")
dff2["Lowercase Rating text"]= dff2['Rating text'].apply(lambda x: x.lower())
print("\nPrinting Shape of the dataframe after adding the Lowercase Rating text column : ")
print(dff2.shape)

print("\nConverting Rating color Column in lowercase")
dff2["Lowercase Rating color"]= dff2['Rating color'].apply(lambda x: x.lower())
print("\nPrinting Shape of the dataframe after adding the Lowercase Rating color column : ")
print(dff2.shape)

print("\nPrinting the new added columns with lowercase values : ")
print(dff2.head())

print("\nConverting the Column Headers in lowercase")
dff2.columns = map(str.lower, dff2.columns)
print("\nPrinting the columns Headers in lowercase now: ")
print(dff2)

輸出

在命令視窗中執行Python檔案。

圖3:使用cmd視窗顯示結果

結論

在這篇Python和Pandas文章中,我們使用三個不同的例子來演示如何將資料框列的值轉換為小寫。在所有三個例子中都使用了不同的函式。在第三個例子中,還介紹了將列標題更改為小寫的方法。

更新於:2023年5月11日

5000+ 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告