FunnyChef FunnyChef - 1 year ago 94
Python Question

Extract only certain rows using value_coutns

I have simple code that reads a CSV file and gives me a list of

but I want to extract only certain rows from the
results. Any suggestions how to do this?

value_count results look like: 79 51 44 43 38

What I would like to be able to do is search through the results and return only those rows that match a certain domain name:

desired results: 79 44 38

Code so far:

import pandas as pd

# read csv into the data dataframe
allData = r'/downloads/data/latest/export-2016-09-30-2039-55502fd6.csv'
tickets_df = pd.read_csv((allData),parse_dates=['Created at'],index_col='Created at')

# Use 2016 data ony
tickets_2016_df = (tickets_df.loc['2016-01-01':'2016-10-20'])

org_counts = tickets_2016_df['Requester domain'].value_counts()
print (org_counts)

Answer Source

you can convert your series to DataFrame and then use .query() method:

In [120]: org_counts
Out[120]:    79     51     44    43    38
Name: val, dtype: int64

In [121]: org_counts.to_frame('count').query("index in ['','','']")
              count     79      44     38

or using Index.isin() method and boolean indexing:

In [122]: domains = ['','','']

In [123]: org_counts[org_counts.index.isin(domains)]
Out[123]:    79     44    38
Name: val, dtype: int64
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download