How do I select and store columns greater than a number in pandas? [duplicate]

2024/11/19 9:41:20

I have a pandas DataFrame with a column of integers. I want the rows containing numbers greater than 10. I am able to evaluate True or False but not the actual value, by doing:

df['ints'] = df['ints'] > 10

I don't use Python very often so I'm going round in circles with this.

I've spent 20 minutes Googling but haven't been able to find what I need....

Edit:

    observationID   recordKey   gridReference   siteKey siteName    featureKey  startDate   endDate pTaxonVersionKey    taxonName   authority   commonName  ints
0   463166539   1767    SM90    NaN NaN 150161  12/02/2006  12/02/2006  NBNSYS0100004720    Pipistrellus pygmaeus   (Leach, 1825)   Soprano Pipistrelle 2006
1   463166623   4325    TL65    NaN NaN 168651  21/12/2008  21/12/2008  NHMSYS0020001355    Pipistrellus pipistrellus sensu stricto (Schreber, 1774)    Common Pipistrelle  2008
2   463166624   4326    TL65    NaN NaN 168651  18/01/2009  18/01/2009  NHMSYS0020001355    Pipistrellus pipistrellus sensu stricto (Schreber, 1774)    Common Pipistrelle  2009
3   463166625   4327    TL65    NaN NaN 168651  15/02/2009  15/02/2009  NHMSYS0020001355    Pipistrellus pipistrellus sensu stricto (Schreber, 1774)    Common Pipistrelle  2009
4   463166626   4328    TL65    NaN NaN 168651  19/12/2009  19/12/2009  NHMSYS0020001355    Pipistrellus pipistrellus sensu stricto (Schreber, 1774)    Common Pipistrelle  2009
Answer

Sample DF:

In [79]: df = pd.DataFrame(np.random.randint(5, 15, (10, 3)), columns=list('abc'))In [80]: df
Out[80]:a   b   c
0   6  11  11
1  14   7   8
2  13   5  11
3  13   7  11
4  13   5   9
5   5  11   9
6   9   8   6
7   5  11  10
8   8  10  14
9   7  14  13

present only those rows where b > 10

In [81]: df[df.b > 10]
Out[81]:a   b   c
0  6  11  11
5  5  11   9
7  5  11  10
9  7  14  13

Minimums (for all columns) for the rows satisfying b > 10 condition

In [82]: df[df.b > 10].min()
Out[82]:
a     5
b    11
c     9
dtype: int32

Minimum (for the b column) for the rows satisfying b > 10 condition

In [84]: df.loc[df.b > 10, 'b'].min()
Out[84]: 11

UPDATE: starting from Pandas 0.20.1 the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers.

https://en.xdnf.cn/q/26454.html

Related Q&A

Plotting transparent histogram with non transparent edge

I am plotting a histogram, and I have three datasets which I want to plot together, each one with different colours and linetype (dashed, dotted, etc). I am also giving some transparency, in order to s…

Cross-platform desktop notifier in Python

I am looking for Growl-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:>>> import desktopnotifier as dn >>> dn.notify(Title, Long description…

set object is not JSON serializable [duplicate]

This question already has answers here:How to JSON serialize sets? [duplicate](12 answers)Closed 9 years ago.When I try to run the following code:import jsond = {testing: {1, 2, 3}} json_string = json…

Python Sqlite3: INSERT INTO table VALUE(dictionary goes here)

I would like to use a dictionary to insert values into a table, how would I do this? import sqlite3db = sqlite3.connect(local.db) cur = db.cursor()cur.execute(DROP TABLE IF EXISTS Media)cur.execute(CR…

Count the uppercase letters in a string with Python

I am trying to figure out how I can count the uppercase letters in a string. I have only been able to count lowercase letters:def n_lower_chars(string):return sum(map(str.islower, string))Example of w…

Multithreading for Python Django

Some functions should run asynchronously on the web server. Sending emails or data post-processing are typical use cases.What is the best (or most pythonic) way write a decorator function to run a func…

run a crontab job using an anaconda env

I want to have a cron job execute a python script using an already existing anaconda python environment called my_env. The only thing I can think to do is have the cron job run a script called my_scri…

pandas - Merging on string columns not working (bug?)

Im trying to do a simple merge between two dataframes. These come from two different SQL tables, where the joining keys are strings:>>> df1.col1.dtype dtype(O) >>> df2.col2.dtype dtyp…

Making a chart bigger in size

Im trying to get a bigger chart. However, the figure method from matplotlib does not seem to be working properly. I get a message, which is not an error: <matplotlib.figure.Figure at 0xa25f7f0>My…

index of non NaN values in Pandas

From Pandas data frame, how to get index of non "NaN" values?My data frame isA b c 0 1 q1 1 1 2 NaN 3 2 3 q2 3 3 4 q1 NaN 4 5 q2 7And I want the…