Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

filter dataframe site:stackoverflow.com

df_filtered = df.query('a > 0 and 0 < b < 2')
Comment

filter dataframe site:stackoverflow.com

df = pd.DataFrame(np.random.randn(30, 3), columns=['a','b','c'])
df_filtered = df.query('a > 0').query('0 < b < 2')
Comment

filter dataframe site:stackoverflow.com

df_filtered = df.query('a > 0 and 0 < b < 2')

#If you need to refer to python variables in your query, the documentation says, "You can refer to variables in the environment by prefixing them with an ‘@’ character like @a + b". Note that the following are valid: df.query('a in list([1,2])'), s = set([1,2]); df.query('a in @s'). – 
#teichert
Comment

filter dataframe site:stackoverflow.com

df[df["column_name"] != 5].groupby("other_column_name")
Comment

filter dataframe site:stackoverflow.com

import pandas as pd
import numpy as np

np.random.seed([3,1415])
df = pd.DataFrame(
    np.random.randint(10, size=(10, 5)),
    columns=list('ABCDE')
)

df

   A  B  C  D  E
0  0  2  7  3  8
1  7  0  6  8  6
2  0  2  0  4  9
3  7  3  2  4  3
4  3  6  7  7  4
5  5  3  7  5  9
6  8  7  6  4  7
7  6  2  6  6  5
8  2  8  7  5  8
9  4  7  6  1  5
Comment

filter dataframe site:stackoverflow.com

df.query('D > B')

   A  B  C  D  E
0  0  2  7  3  8
1  7  0  6  8  6
2  0  2  0  4  9
3  7  3  2  4  3
4  3  6  7  7  4
5  5  3  7  5  9
7  6  2  6  6  5
Comment

filter dataframe site:stackoverflow.com

df.query('D > B').query('C > B')
# equivalent to
# df.query('D > B and C > B')
# but defeats the purpose of demonstrating chaining

   A  B  C  D  E
0  0  2  7  3  8
1  7  0  6  8  6
4  3  6  7  7  4
5  5  3  7  5  9
7  6  2  6  6  5
Comment

filter dataframe site:stackoverflow.com

df_filtered = df.loc[lambda x: x['column'] == value]
Comment

filter dataframe site:stackoverflow.com

df_filtered = df.pipe(lambda x: x['column'] == value)
Comment

filter pandas stack overflow

uniquevalues = np.unique(df[['id']].values)
Comment

filter dataframe site:stackoverflow.com

df_filtered = df.query('a > 0 and 0 < b < 2')
Comment

filter dataframe site:stackoverflow.com

df = pd.DataFrame(np.random.randn(30, 3), columns=['a','b','c'])
df_filtered = df.query('a > 0').query('0 < b < 2')
Comment

filter dataframe site:stackoverflow.com

df_filtered = df.query('a > 0 and 0 < b < 2')

#If you need to refer to python variables in your query, the documentation says, "You can refer to variables in the environment by prefixing them with an ‘@’ character like @a + b". Note that the following are valid: df.query('a in list([1,2])'), s = set([1,2]); df.query('a in @s'). – 
#teichert
Comment

filter dataframe site:stackoverflow.com

df[df["column_name"] != 5].groupby("other_column_name")
Comment

filter dataframe site:stackoverflow.com

import pandas as pd
import numpy as np

np.random.seed([3,1415])
df = pd.DataFrame(
    np.random.randint(10, size=(10, 5)),
    columns=list('ABCDE')
)

df

   A  B  C  D  E
0  0  2  7  3  8
1  7  0  6  8  6
2  0  2  0  4  9
3  7  3  2  4  3
4  3  6  7  7  4
5  5  3  7  5  9
6  8  7  6  4  7
7  6  2  6  6  5
8  2  8  7  5  8
9  4  7  6  1  5
Comment

filter dataframe site:stackoverflow.com

df.query('D > B')

   A  B  C  D  E
0  0  2  7  3  8
1  7  0  6  8  6
2  0  2  0  4  9
3  7  3  2  4  3
4  3  6  7  7  4
5  5  3  7  5  9
7  6  2  6  6  5
Comment

filter dataframe site:stackoverflow.com

df.query('D > B').query('C > B')
# equivalent to
# df.query('D > B and C > B')
# but defeats the purpose of demonstrating chaining

   A  B  C  D  E
0  0  2  7  3  8
1  7  0  6  8  6
4  3  6  7  7  4
5  5  3  7  5  9
7  6  2  6  6  5
Comment

filter dataframe site:stackoverflow.com

df_filtered = df.loc[lambda x: x['column'] == value]
Comment

filter dataframe site:stackoverflow.com

df_filtered = df.pipe(lambda x: x['column'] == value)
Comment

filter pandas stack overflow

uniquevalues = np.unique(df[['id']].values)
Comment

PREVIOUS NEXT
Code Example
Python :: django logout user 
Python :: series.Series to dataframe 
Python :: replace value pandas df 
Python :: create or update django models 
Python :: numpy add one column 
Python :: logistic regression algorithm in python 
Python :: clean punctuation from string python 
Python :: write data to using pickle 
Python :: generate n different random numbers python 
Python :: pandas create new column conditional on other columns 
Python :: get guild by id discord.py 
Python :: remove first character from string python 
Python :: get sum in range 
Python :: python binary tree 
Python :: what is // in python 
Python :: plot sphere in matplotlib 
Python :: python find in largest 3 numbers in an array 
Python :: creating a virtual environment with django on windows 
Python :: correlation analysis of dataframe python 
Python :: tkinter text blurry 
Python :: distplot with plotly 
Python :: python program to convert unit 
Python :: Adjusting Subplot Margins in Matplotlib 
Python :: random number pythob 
Python :: python check if class has function 
Python :: How to recursively sort the elements of a stack, in Python? 
Python :: python convert string to lowercase 
Python :: python file.write is not writing whole line 
Python :: read files and write into another files python 
Python :: make screen shot of specific part of screen python 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =