Pandas 如何将数据帧中的列名改为小写

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文件

Pandas 如何将数据帧中的列名改为小写

图:这个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文件

Pandas 如何将数据帧中的列名改为小写

图1:使用cmd窗口显示结果。

示例2:使用map(str.lower)函数将数据帧列内容转换为小写

设计步骤和编码

  • 步骤1 − 首先导入pandas。现在读取zomato.csv文件作为数据集,该数据集将用于将其加载到数据帧中。

  • 步骤2 − 创建一个名为dff2的数据帧,并使用pandas的read_csv函数读取CSV文件。只需使用usecols选择两列,’Restaurant Name’和’Rating text’。

  • 步骤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文件

Pandas 如何将数据帧中的列名改为小写

图2:使用cmd窗口显示结果。

示例3:更改列标题和内容为小写

设计步骤和编码

  • 步骤 1 − 首先导入pandas。现在读取zomato.csv文件,将其作为给定的数据集用于加载到数据框中。

  • 步骤 2 − 创建一个名为dff2的数据框,并利用pandas的read_csv函数读取CSV文件。使用usecols选择三列,’Restaurant Name’,’Rating text’和’Rating color’。

  • 步骤 3 − 使用delimiter = ‘,’和zomato.csv的路径。使用head函数打印该数据框的一些行和列。

  • 步骤 4 − 在dff2的’Rating text’上使用apply(lambda x: x.lower())将其转换为小写。将这个小写列添加到数据框中。

  • 步骤 5 − Now在“Rating color”列上使用步骤 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文件。

Pandas 如何将数据帧中的列名改为小写

图3:使用cmd窗口显示结果

结论

在Python和Pandas文章中,我们使用了三个不同的示例,以演示如何将数据框列的值转换为小写的方法。这三个示例中都使用了不同的函数。在第三个示例中,还介绍了将列标题转换为小写的方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程

Pandas 精选笔记