python pandas字符串过滤

import pandas as pd
df = pd.read_csv("together.csv" , index_col=False)

#新建motor group fr lr
df["motor"] = "#"
df["group"] = "0"  #默认不是组合
df["fr"] = "#"
df["lr"] = "#"
#打标签motor
#找出motor 中含某些字符串的然后将对应的motor值改变
df["product_name"]=df["product_name"].str.lower()  #将name字段全部换成小写
a = (df.product_name.str.find("with motor") != -1)
b = (df.product_name.str.find("w/motor") != -1)
c = (df.product_name.str.find("w/ motor") != -1)
d = (df.product_name.str.find("& motor") != -1)
e = (df.product_name.str.find("power") != -1)
pattern = a| b | c | d| e
df.loc[pattern , "motor"] = "1"

a = (df.product_name.str.find("without motor") != -1)
b = (df.product_name.str.find("manual") != -1)
pattern = a| b
df.loc[pattern, "motor"] = "0"
# print(df[df["motor"] == "1"].describe())#查看信息
# print(df[df["motor"] == "0"].describe())
# print(df[df["motor"] == "#"].describe())

#打标签 group 默认是0
a = (df.product_name.str.find("pair") != -1)
b = (df.product_name.str.find("set") != -1)
c = (df.product_name.str.find("2x") != -1)
d = (df.product_name.str.find("of 2") != -1)
pattern = a| b | c | d
df.loc[pattern , "group"] = "1"
# print(df[df.group == "1"].describe())
# print(df[df.group == "0"].describe())


#打标签 前后 默认#
a = (df.product_name.str.find("front") != -1)
df.loc[a , "fr"] = "f"
a = (df.product_name.str.find("rear") != -1)
df.loc[a , "fr"] = "r"
# print(df[df.fr == "f"].describe())
# print(df[df.fr == 'r'].describe())
# print(df[df.fr == '#'].describe())
#打标签 左右 默认#
a = (df.product_name.str.find("right") != -1)
b = (df.product_name.str.find("rh") != -1)
c = (df.product_name.str.find("passenger") != -1)
pattern = a| b | c
df.loc[pattern , "lr"] = "r"
a = (df.product_name.str.find("left") != -1)
b = (df.product_name.str.find("lh") != -1)
c = (df.product_name.str.find("driver") != -1)
pattern = a| b | c
df.loc[pattern , "lr"] = "l"

# print(df[df.lr == "l"].describe())
# print(df[df.lr == "r"].describe())
# print(df[df.lr == "#"].describe())


# print(df)
df.to_csv("together_tag.csv" , index=False)








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值