我有一个看起来像这样的数据集:
Id Product Age
001 soup,macaroni, 23
002 bread,milk,butter, 18
003 Bread, 20
我想删除每个产品末尾的逗号,使其看起来像这样:
Id Product Age
001 soup,macaroni 23
002 bread,milk,butter 18
003 Bread 20
答案 0 :(得分:1)
使用str.rstrip(",")
例如:
import pandas as pd
df = pd.DataFrame({"Product": ["soup,macaroni,", "bread,milk,butter,", "Bread,"]})
df["Product"] = df["Product"].str.rstrip(",")
print(df)
输出:
Product
0 soup,macaroni
1 bread,milk,butter
2 Bread
答案 1 :(得分:0)
让我们假设您的数据框为df。
执行以下操作
df["Product"] = df["Product"].apply(lambda x:x.strip(","))
答案 2 :(得分:0)
可以str.slice
直到最后一个值
df.Product.str.slice(0, -1)
0 soup,macaroni
1 bread,milk,butter
2 Bread
Name: Product, dtype: object