1

There are 2 columns for example...

This is my original data frame

quote: ['Originally Posted by Gilly Is it? Hmmmm. I enjoy randomness. It changes my mood. I guess not everyone responds the same way. ;)']

all_post: ['Originally Posted by Gilly Is it? Hmmmm. I enjoy randomness. It changes my mood. I guess not everyone responds the same way. ;) Ah ok.. maybe I didn't understand.. /animal crawls back in its hole']

I would like to separate the sentence

"Ah ok.. maybe I didn't understand.. /animal crawls back in its hole"

into another column

This is what I have tried

def extract_post(Quote,Post):

    post = [x for x in all_post if x not in quote]

    return post

for j,row in confession.iterrows():
    if type(row['quote']) == float:
        continue
    else:
        print(extract_post(row['quote'], row['all_post']))

But the result for the 2nd was ['A', 'k', "'", '/', 'k'] (basically it compares each character)

Flora
  • 13
  • 3

2 Answers2

1

You just want to remove quote and the space after it from all_post. We don't need to define a function for this simple task.

        print(row['all_post'].replace(row['quote']+" ", ""))
Armali
  • 18,255
  • 14
  • 57
  • 171
  • 1
    Oh, that's right. Thank you. I'm a beginner so I'm still confused of what to do sometimes. Thanks :D – Flora Nov 10 '19 at 06:14
0

The tricky bit is escaping any of the special characters you might encounter. This could be an issue if the resulting comment you are trying to extract has those special characters, and you would like to keep them.

In the code below, the parentheses needed to be escaped by replacing it with ')' in order for this to work. If they were not escaped there could be unbalanced parentheses for example.

confession = pd.DataFrame(columns=['quote', 'all_post'])
confession = confession.append(pd.Series({'quote':"Originally Posted by Gilly Is it? Hmmmm. I enjoy randomness. It changes my mood. I guess not everyone responds the same way. ;)",'all_post':"Originally Posted by Gilly Is it? Hmmmm. I enjoy randomness. It changes my mood. I guess not everyone responds the same way. ;) Ah ok.. maybe I didn't understand.. /animal crawls back in its hole"}), ignore_index=True)

for index,row in confession.iterrows():
    quote = confession['quote'].iloc[index].replace(')','\)')
    post = confession['all_post'].iloc[index].replace(')','\)')
    comment = post.replace(f'{quote}', '').strip()
    confession.loc[index, 'comment'] = comment

You will have to consider all special characters and escape them in a better way than what I have suggested in the example. This answer on How to escape special characters of a string with single backslashes may help identify any escape characters you may encounter in the kinds of quotes you are dealing with.

Rick
  • 347
  • 4
  • 16