Python 在Pandas DataFrame中改变列名和行索引

Python 在Pandas DataFrame中改变列名和行索引

给定一个Pandas DataFrame,让我们看看如何改变它的列名和行索引。

关于Pandas DataFrame
Pandas DataFrame是用来存储数据的矩形网格。当存储在dataFrame中时,很容易将数据可视化并进行处理。

  • 它由行和列组成。
  • 每一行都是对某个实例的测量,而列是一个向量,包含了某些特定属性/变量的数据。
  • 每个数据框架列在任何特定的列中都有同质的数据,但数据框架行在任何特定的行中都可以包含同质或异质的数据。
  • 与二维数组不同,pandas数据框架的轴是有标签的。

Pandas Dataframe类型有两个属性,叫做 “列 “和 “索引”,可以用来改变列名以及行索引。

使用dictionary.创建一个DataFrame。

# first import the libraries
import pandas as pd
   
# Create a dataFrame using dictionary
df=pd.DataFrame({"Name":['Tom','Nick','John','Peter'],
                 "Age":[15,26,17,28]})
  
# Creates a dataFrame with
# 2 columns and 4 rows
df
Python

Python 在Pandas DataFrame中改变列名和行索引

1.方法#1:使用df.columns和df.index属性改变列名和行索引。

为了改变列名,我们提供一个包含列名的Python列表 df.columns= [‘First_col’, ‘Second_col’, ‘Third_col’, …..] 。
为了改变行索引,我们还提供了一个python列表给它 df.index=[‘row1’, ‘row2’, ‘row3’, ……] .
“`python # Let’s rename already created dataFrame.
  
# Check the current column names
# using "columns" attribute.
# df.columns
  
# Change the column names
df.columns =['Col_1', 'Col_2']
  
# Change the row indexes
df.index = ['Row_1', 'Row_2', 'Row_3', 'Row_4']
  
# printing the data frame
df

<pre><code class=""><br />![Python 在Pandas DataFrame中改变列名和行索引](https://static.deepinout.com/geekdocs/2022/10/20221014083246-2.png "Python 在Pandas DataFrame中改变列名和行索引")

2.方法#2:使用带有字典的rename()函数来改变一个单列
“`python # let’s change the first column name
# from “A” to “a” using rename() function
df = df.rename(columns = {“Col_1″:”Mod_col”})
  
df

Python 在Pandas DataFrame中改变列名和行索引

同时改变多个列名 –
“`python # We can change multiple column names by 
# passing a dictionary of old names and 
# new names, to the rename() function.
df = df.rename({"Mod_col":"Col_1","B":"Col_2"}, axis='columns')
  
df

<pre><code class=""><br />![Python 在Pandas DataFrame中改变列名和行索引](https://static.deepinout.com/geekdocs/2022/10/20221014083246-4.png "Python 在Pandas DataFrame中改变列名和行索引")

3.方法#3:使用Lambda函数来重命名列。

lambda函数是一个小型匿名函数,它可以接受任何数量的参数,但只能有一个表达式。使用lambda函数,我们可以一次性地修改所有的列名。让我们用lambda函数在每个列名的后面加上'x'。
“`python df = df.rename(columns=lambda x: x+’x’)
  
# this will modify all the column names
df

Python 在Pandas DataFrame中改变列名和行索引

4.方法#4:使用数值属性来重命名列。

我们可以直接在我们想改变名称的列上使用数值属性。
“`python df.columns.values[1] = ‘Student_Age’
  
# this will modify the name of the first column
df

<pre><code class=""><br />![Python 在Pandas DataFrame中改变列名和行索引](https://static.deepinout.com/geekdocs/2022/10/20221014083246-6.png "Python 在Pandas DataFrame中改变列名和行索引")

让我们使用Lambda函数来改变行索引。
“`python # To change the row indexes
df = pd.DataFrame({“A”:[‘Tom’,’Nick’,’John’,’Peter’],
                   “B”:[25,16,27,18]})
  
# this will increase the row index value by 10 for each row
df = df.rename(index = lambda x: x + 10)
  
df

Python 在Pandas DataFrame中改变列名和行索引

现在,如果我们想同时改变行的索引和列的名称,那么可以使用rename()函数来实现,并将列和索引属性作为参数传递。
python df = df.rename(index = lambda x: x + 5,
               columns = lambda x: x +'x')
   
# increase all the row index label by value 5
# append a value 'x' at the end of each column name. 
df

Python 在Pandas DataFrame中改变列名和行索引

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册