创建一个管道并从已经创建的 DataFrame 中移除一行 - Python Pandas
使用 pdpipe 库的 **ValDrop() 方法从已创建的 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] } )
现在,使用 valdDrop() 方法删除一行 −
dataFrame = pdp.ValDrop(['Jaguar'],'Car').apply(dataFrame)
示例
以下是完整的代码 −
import pdpipe as pdp import pandas as pd # function to check for excess units def demo(x): if x >= 100: return "OverStock" else: return "UnderStock" # Create DataFrame 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" and its values based on an already created column "Units" dataFrame['Stock'] = dataFrame['Units'].apply(demo) print("\n DataFrame with a new column...\n",dataFrame) # removing a row with 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
广告