创建管道并从 DataFrame 中删除一列 - Python Pandas
使用 pdpipe 库的 colDrop() 方法从 Pandas DataFrame 中删除一列。首先,使用它们各自的别名导入必需的 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() 方法。这里,我们正在删除“Units”列——
resDF = pdp.ColDrop("Units").apply(dataFrame)
示例
以下是完整代码——
import pdpipe as pdp import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90] } ) print("DataFrame ...\n",dataFrame) # removing a row with pdp dataFrame = pdp.ValDrop(['Jaguar'],'Car').apply(dataFrame) print("\n DataFrame after removing a row...\n",dataFrame) # removing a column with 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
廣告