创建一个 Pipeline 并从已创建的 DataFrame 中删除一行 – Python Pandas

创建一个 Pipeline 并从已创建的 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)

更多Pandas文章,请阅读:Pandas教程

示例

以下是完整代码 –

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程