Pythonic way to filter columns and then create a new column

Viewed 93

I have a .xlsx file that I am opening with this code:

import pandas as pd

df = pd.read_excel(open('file.xlsx','rb'))
df['Description'].head

and I have the following result, which looks pretty good.

ID     | Description
:----- | :-----------------------------
0      | Some Description with no hash
1      | Text with #one hash
2      | Text with #two #hashes

Now I want to create a new column, keeping only words started with #, like this one:

ID     | Description                      |  Only_Hash
:----- | :-----------------------------   |  :-----------------
0      | Some Description with no hash    |   Nan
1      | Text with #one hash              |   #one
2      | Text with #two #hashes           |   #two #hashes

I was able to count/separate lines with #:

descriptionWithHash = df['Description'].str.contains('#').sum()

but now I want to create the column like I described above. What is the easiest way to do that?

Regards!

PS: it is supposed to show a table format in the question but I can't figure out why it is showing wrong!

2 Answers
Related