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)
python pandas字符串过滤
最新推荐文章于 2024-05-20 16:08:59 发布