0

I want to remove a certain keywords or string in a column from pandas dataframe.

The dataframe df looks like this:

YEAR    WEEK
2019    WK-01
2019    WK-02
2019    WK-03
2019    WK-14
2019    WK-25
2020    WK-06
2020    WK-07

I would like to remove WK-and 0 from the WEEK column so that my output will looks like this:

YEAR    WEEK
2019    1
2019    2
2019    3
2019    14
2019    25
2020    6
2020    7

2 Answers 2

3

You can try:

df['WEEK'] = df['WEEK'].str.extract('(\d*)$').astype(int)

Output:

   YEAR  WEEK
0  2019     1
1  2019     2
2  2019     3
3  2019    14
4  2019    25
5  2020     6
6  2020     7
Sign up to request clarification or add additional context in comments.

Comments

3

Shave off the first three characters and convert to int.

df['WEEK'] = df['WEEK'].str[3:].astype(int)

3 Comments

what's the different between using the str.extract and only str?
@NurAtiqah str.extract lets you extract matches from regular expressions. str[...] allows vectorized access to specific elements or slices of elements.
noted...so..if I were to combine this output to the original dataframe that has multiple column...I can use join() right?

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.