📜  Pandas 中的 inplace 是什么意思?

📅  最后修改于: 2022-05-13 01:54:53.020000             🧑  作者: Mango

Pandas 中的 inplace 是什么意思?

在本文中,我们将看到 Pandas 中的 Inplace。 Inplace 是在不同函数中使用的参数。一些将 inplace 用作属性的函数,例如set_index()、 dropna() fillna() reset_index() 、drop()、replace() 等等。此属性的默认值为False ,它返回对象的副本。

这里我们使用fillna()方法。

让我们通过逐步实现来理解这个方法:

步骤 1.首先,我们导入所有必需的库。

Python3
# import required module
import pandas as pd


Python3
# creating dataframe
dataframe = pd.DataFrame({'Name':['Shobhit','vaibhav',
                                'vimal','Sourabh'],
                          
                        'Class':[11,12,10,9],
                        'Age':[18,20,21,17]})
  
# Checking created dataframe
display(dataframe)


Python3
# without using inplace renaming the column
new_data = dataframe.rename(columns = {'Name':'FirstName'})
  
# check new_data
display(new_data)


Python3
# putting inplace=False
new_data_2 = dataframe.rename(columns = {'Name':'FirstName'},
                            inplace = False)
  
#check new_data_2
display(new_data_2)


Python3
# Putting Inplace=True
dataframe.rename(columns = {'Name':'FirstName'},
                 inplace = True)
   
# check whether dataframe is modidfied or not
print(dataframe)


Python3
# importing pandas
import pandas as pd
  
# creating dataframe
dataframe=pd.DataFrame({'Name':['Shobhit','Vaibhav',
                                'Vimal','Sourabh'],
                          
                        'Class':[11,12,10,9],
                        'Age':[18,20,21,17]})
  
# Checking created dataframe
# copied dataframe
display(dataframe)
  
# without using inplace renaming the column
new_data = dataframe.rename(columns = {'Name':'FirstName'})
  
# Copied dataframe
display(new_data)  
  
# checking whether dataframe is modified or not
# Original dataframe
display(dataframe)
  
# putting inplace=False
new_data_2 = dataframe.rename(columns = {'Name':'FirstName'},
                              inplace = False)
  
# Copied dataframe
display(new_data_2)
  
# checking whether dataframe is modified or not
# Original dataframe
display(dataframe)
  
# Putting Inplace=True
dataframe.rename(columns = {'Name':'FirstName'},
                 inplace = True)
  
# checking whether dataframe is modified or not
# Original dataframe
display(dataframe)


第 2步。创建数据框。

蟒蛇3

# creating dataframe
dataframe = pd.DataFrame({'Name':['Shobhit','vaibhav',
                                'vimal','Sourabh'],
                          
                        'Class':[11,12,10,9],
                        'Age':[18,20,21,17]})
  
# Checking created dataframe
display(dataframe)

输出 :

步骤 3.要查看就地使用,我们将使用重命名函数将“Name”列重命名为“FirstName” 

在这一步中,我们不会在我们的代码中使用 inplace。

蟒蛇3

# without using inplace renaming the column
new_data = dataframe.rename(columns = {'Name':'FirstName'})
  
# check new_data
display(new_data)

输出 :

我们可以清楚地看到原始数据帧没有变化。由此,我们得出结论,inplace 的默认值是 False。

现在在这一步中,我们将使用带有False值的 inplace。

蟒蛇3

# putting inplace=False
new_data_2 = dataframe.rename(columns = {'Name':'FirstName'},
                            inplace = False)
  
#check new_data_2
display(new_data_2)

输出 :

我们再次可以清楚地看到原始数据集没有变化。

最后,我们放置了等于True的值。      

蟒蛇3

# Putting Inplace=True
dataframe.rename(columns = {'Name':'FirstName'},
                 inplace = True)
   
# check whether dataframe is modidfied or not
print(dataframe)

输出 :

最后,我们可以看到原始数据框列已从“Name”修改为“FirstName”。

以下是基于上述方法的完整程序:

蟒蛇3

# importing pandas
import pandas as pd
  
# creating dataframe
dataframe=pd.DataFrame({'Name':['Shobhit','Vaibhav',
                                'Vimal','Sourabh'],
                          
                        'Class':[11,12,10,9],
                        'Age':[18,20,21,17]})
  
# Checking created dataframe
# copied dataframe
display(dataframe)
  
# without using inplace renaming the column
new_data = dataframe.rename(columns = {'Name':'FirstName'})
  
# Copied dataframe
display(new_data)  
  
# checking whether dataframe is modified or not
# Original dataframe
display(dataframe)
  
# putting inplace=False
new_data_2 = dataframe.rename(columns = {'Name':'FirstName'},
                              inplace = False)
  
# Copied dataframe
display(new_data_2)
  
# checking whether dataframe is modified or not
# Original dataframe
display(dataframe)
  
# Putting Inplace=True
dataframe.rename(columns = {'Name':'FirstName'},
                 inplace = True)
  
# checking whether dataframe is modified or not
# Original dataframe
display(dataframe)

输出 :