创建管道并从已创建的 DataFrame 中删除一行 - Python Pandas
使用pdpipe库的ValDrop()方法从已经创建的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] } )
现在,使用valdDrop()方法删除一行-
dataFrame = pdp.ValDrop(['Jaguar'],'Car').apply(dataFrame)
示例
以下是完整的代码-
import pdpipe as pdp import pandas as pd #检查多余单位的功能 def demo(x): if x >= 100: return "OverStock" else: return "UnderStock" #创建数据帧 dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90] } ) print("DataFrame ...\n",dataFrame) # adding a new column "Stock" 及其基于已创建列的值 "Units" dataFrame['Stock'] = dataFrame['Units'].apply(demo) print("\n DataFrame with a new column...\n",dataFrame) #用pdp删除一行 dataFrame = pdp.ValDrop(['Jaguar'],'Car').apply(dataFrame) print("\n DataFrame after removing a row...\n",dataFrame)输出结果
这将产生以下输出-
DataFrame ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Mustang 80 4 Bentley 110 5 Jaguar 90 DataFrame with a new column... Car Units Stock 0 BMW 100 OverStock 1 Lexus 150 OverStock 2 Audi 110 OverStock 3 Mustang 80 UnderStock 4 Bentley 110 OverStock 5 Jaguar 90 UnderStock DataFrame after removing a value... Car Units Stock 0 BMW 100 OverStock 1 Lexus 150 OverStock 2 Audi 110 OverStock 3 Mustang 80 UnderStock 4 Bentley 110 OverStock