0

Here is a column of my dataframe.

enter image description here

I want to select for example for the first image 28g and delete (1 ONZ). how can I do ?

3
  • 3
    Learn how to ask questions. Don't provide images for data. Commented Dec 7, 2018 at 2:02
  • 1
    It is always recommended to post your samples in text format NOT in image formats, kindly edit your post by posting sample input and expected output sample in code tags. Commented Dec 7, 2018 at 2:12
  • Based on your comments to the answers, you want 28, not 28g. Please update the question accordingly. it is misleading. Commented Dec 7, 2018 at 3:02

3 Answers 3

2

Use replace:

df.serving_size.str.replace('\(.*','')

0     NaN
1    28g 
2    28g 
3    28g
4    35g
5    52g
Name: serving_size, dtype: object
Sign up to request clarification or add additional context in comments.

2 Comments

tell me you know how I can do to remove the "g" from the "28 g"?
Here df.serving_size.str.replace('g \(.*','')
1

You can extract everything outside of the parentheses:

# original df
>>> df
      serving_size
0              NaN
1     28 g (1 ONZ)
2  28 g (0.25 cup)

>>> df.serving_size.str.extract('(.*)\(.*\)')
0      NaN
1    28 g 
2    28 g 
Name: serving_size, dtype: object

1 Comment

@ sacul thank you. but tell me you know how I can do to remove the "g" from the "28 g"?
1

Using split

df.serving_size.str.split('(').str[0]
0    NaN
1    28g
2    28g
Name: serving_size, dtype: object

2 Comments

@ W-B thank you, but tell me you know how I can do to remove the "g" from the "28 g"?
@G.M df.serving_size.str.split(' ').str[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.