Slice Pandas DataFrame by Row

Viewed 96855

I am working with survey data loaded from an h5-file as hdf = pandas.HDFStore('Survey.h5') through the pandas package. Within this DataFrame, all rows are the results of a single survey, whereas the columns are the answers for all questions within a single survey.

I am aiming to reduce this dataset to a smaller DataFrame including only the rows with a certain depicted answer on a certain question, i.e. with all the same value in this column. I am able to determine the index values of all rows with this condition, but I can't find how to delete this rows or make a new df with these rows only.

4 Answers

If you just need to get the top rows; you can use df.head(10)

Use query to search for specific conditions:

In [3]: df
Out[3]: 
   age family   name
0    1      A   john 
1   36      A  jason 
2   32      A   jane 
3   26      B   jack 
4   30      B  james 

In [4]: df.query('age > 30 & family == "A"')
Out[4]: 
   age family   name
1   36      A  jason 
2   32      A   jane
Related