Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas series: Only keep entries that contain a comma

I scraped a table of SEC filings and extract a specific row as a pandas Series.

The tables are not very standardized in their formatting which makes scraping quite hard as unwanted information is extracted as well.

Take for example the following series I scraped from a table:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

series = {'A': "3,360,003|", 'B': "(17) |", 'C': "16.8|"}
series = pd.Series(data=series, index=['A', 'B', 'C'])

The only information that is relevant for me is the one that contains commas. Is there a way to remove all other entries of the series that doesn’t contain commas?

There may be cases where there is more than one entry with commas, e.g.

series = {'A': "3,360,003|", 'B': "(17,424,32) |", 'C': "16.8|"}
series = pd.Series(data=series, index=['A', 'B', 'C'])

in this case, the first entry that contains commas should be kept while all other should be removed.

Help is much appreciated

>Solution :

Use .str.contains() as a boolean indexer;

s = series[series.str.contains(',', na=False)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading