0

I have a dataframe, df:

data = [{0: 18, 1: '(Responses) 17th Nov 20'},
        {0: 304, 1: '(Responses) 17th Nov 20'},
        {0: 1177, 1: '(Responses) 17th Nov 20'},
        {0: 899, 1: '(Responses) 17th Nov 20'}]

df = pd.DataFrame(data)

0                1                                          
18    (Responses) 17th Nov 20
304   (Responses) 17th Nov 20
1177  (Responses) 17th Nov 20
899   (Responses) 17th Nov 20

Is there any efficient way to extract out 17th Nov 2020 and make it to a new column[2] as 17-11-2020 as date?

It can also be 1st or 2nd or 3rd for other date.

Expected output:

0                1                 2                         
18    (Responses) 17th Nov 20   17-11-2020
304   (Responses) 17th Nov 20   17-11-2020
1177  (Responses) 17th Nov 20   17-11-2020
899   (Responses) 17th Nov 20   17-11-2020
3
  • df[1].str.split(n=1, expand=True)? Commented Dec 28, 2020 at 3:54
  • If we can see how the column was created, it might be possible to provide a more optimal solution that avoids this awkward uncleaned data. Commented Dec 28, 2020 at 3:54
  • @LainTaljuk n=1 it only splits once. Commented Dec 28, 2020 at 4:01

2 Answers 2

1

Try using str.split and pd.to_datetime:

df[2] = pd.to_datetime(df[1].str.replace('\(Responses\) ', ''))
print(df)

Output:

      0                        1          2
0    18  (Responses) 17th Nov 20 2020-11-17
1   304  (Responses) 17th Nov 20 2020-11-17
2  1177  (Responses) 17th Nov 20 2020-11-17
3   899  (Responses) 17th Nov 20 2020-11-17
Sign up to request clarification or add additional context in comments.

6 Comments

Im getting Nan value instead
@user6308605 I edited my answer, please check again
I realised why I get Nan. The actual string is Copy of BeAChampion (Responses) so it should be str[4], correct? But still getting Nan
@user6308605 I have a new edit on my answer check it out
@user6308605 We shouldn't use split, see my example of replace
|
0

Just split your string with "(responses)" as your keyword, and then get the second element after split:

  df['new_column'] = df['1'].str.split("(responses)").str[1]

Comments

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.