创建管道并从 DataFrame 中删除一列 - Python Pandas
使用colDrop()pdpipe库的方法从PandasDataFrame中删除一列。首先,导入所需的pdpipe和pandas库及其各自的别名-
import pdpipe as pdp import pandas as pd
让我们创建一个DataFrame。在这里,我们有两列-
dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90] } )
要从DataFrame中删除列,请使用ColDrop()方法。在这里,我们将删除“单位”列-
resDF = pdp.ColDrop("Units").apply(dataFrame)
示例
以下是完整的代码-
import pdpipe as pdp import pandas as pd #创建数据帧 dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90] } ) print("DataFrame ...\n",dataFrame) #用pdp删除一行 dataFrame = pdp.ValDrop(['Jaguar'],'Car').apply(dataFrame) print("\n DataFrame after removing a row...\n",dataFrame) #使用pdp删除列 resDF = pdp.ColDrop("Units").apply(dataFrame) print("\nDataFrame after removing a column...\n",resDF)输出结果
这将产生以下输出-
DataFrame ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Mustang 80 4 Bentley 110 5 Jaguar 90 Displaying after removing a row... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Mustang 80 4 Bentley 110 Displaying after removing a column... Car 0 BMW 1 Lexus 2 Audi 3 Mustang 4 Bentley