根据"Fruit"列中的多个值删除所有行的方法有多种,以下是其中几种常见的方法:
drop()
函数删除包含指定值的行,可以通过传递一个列表来删除多个值。示例代码:
import pandas as pd
df = pd.read_csv("data.csv")
fruits_to_delete = "apple", "banana", "orange"
df = df[~df"Fruit".isin(fruits_to_delete)]
df.to_csv("modified_data.csv", index=False)
DELETE
语句删除包含指定值的行,可以通过使用IN
关键字来删除多个值。示例代码:
DELETE FROM table_name
WHERE Fruit IN ('apple', 'banana', 'orange');
示例代码:
import csv
with open("data.csv", "r") as file, open("modified_data.csv", "w", newline="") as new_file:
reader = csv.reader(file)
writer = csv.writer(new_file)
# 逐行判断并写入新的CSV文件
fruits_to_delete = ["apple", "banana", "orange"]
for row in reader:
if row[0] not in fruits_to_delete:
writer.writerow(row)
以上是根据"Fruit"列中的多个值删除所有行的几种常见方法,具体选择哪种方法取决于数据集的存储方式和个人偏好。
领取专属 10元无门槛券
手把手带您无忧上云