I get this data frame:
Item .................
0 Banana (From Spain)...
1 Chocolate ............
2 Apple (From USA) .....
............
And I want change all Item's names by removing the parenthesis, getting finally
Item .................
0 Banana ...............
1 Chocolate ............
2 Apple ................
............
I thought, I should use replace but there are too much data so I'm thinking in use something like
import re
for i in dataframe.index:
if bool(re.search('.*\(.*\).*', dataframe.iloc[i]["Item"])):
dataframe.ix[i,"Item"] = dataframe.iloc[i]["Item"].split(" (")[0]
But I'm not sure if is the most efficient way.
You can use str.replace
by regex
with str.strip
if need remove last whitespaces:
df.Item = df.Item.str.replace(r"\(.*\)","").str.strip()
print (df)
Item
0 Banana
1 Chocolate
2 Apple
Another simplier solution with str.split
with indexing with str :
df.Item = df.Item.str.split(' \(').str[0]
print (df)
Item
0 Banana
1 Chocolate
2 Apple
这可以解决问题:
df.Item = df.Item.apply(lambda x: x.split(" (")[0])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.