CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2019-02-22
by Gary

Original Post

Original - Posted on 2013-06-12
by unutbu



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I will take the existing example from this post, https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas:
import pandas as pd import numpy as np df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(), 'B': 'one one two three two two one three'.split(), 'C': np.arange(8), 'D': np.arange(8) * 2}) print(df) # A B C D # 0 foo one 0 0 # 1 bar one 1 2 # 2 foo two 2 4 # 3 bar three 3 6 # 4 foo two 4 8 # 5 bar two 5 10 # 6 foo one 6 12 # 7 foo three 7 14

Now given the above dataset I am looking for an efficient way to return all rows containing a value from any column matching on a regex.
For example,
a search on '1[2,4]|three' should return
3 bar three 3 6 6 foo one 6 12 7 foo three 7 14
To select rows whose column value equals a scalar, `some_value`, use `==`:
df.loc[df['column_name'] == some_value]
To select rows whose column value is in an iterable, `some_values`, use `isin`:
df.loc[df['column_name'].isin(some_values)]
Combine multiple conditions with `&`:
df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)]
Note the parentheses. Due to Python's [operator precedence rules](https://docs.python.org/3/reference/expressions.html#operator-precedence), `&` binds more tightly than `<=` and `>=`. Thus, the parentheses in the last example are necessary. Without the parentheses
df['column_name'] >= A & df['column_name'] <= B
is parsed as
df['column_name'] >= (A & df['column_name']) <= B
which results in a [Truth value of a Series is ambiguous error][1].
----------
To select rows whose column value *does not equal* `some_value`, use `!=`:
df.loc[df['column_name'] != some_value]

`isin` returns a boolean Series, so to select rows whose value is *not* in `some_values`, negate the boolean Series using `~`:
df.loc[~df['column_name'].isin(some_values)]
----------
For example,
import pandas as pd import numpy as np df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(), 'B': 'one one two three two two one three'.split(), 'C': np.arange(8), 'D': np.arange(8) * 2}) print(df) # A B C D # 0 foo one 0 0 # 1 bar one 1 2 # 2 foo two 2 4 # 3 bar three 3 6 # 4 foo two 4 8 # 5 bar two 5 10 # 6 foo one 6 12 # 7 foo three 7 14
print(df.loc[df['A'] == 'foo'])
yields
A B C D 0 foo one 0 0 2 foo two 2 4 4 foo two 4 8 6 foo one 6 12 7 foo three 7 14
----------
If you have multiple values you want to include, put them in a list (or more generally, any iterable) and use `isin`:
print(df.loc[df['B'].isin(['one','three'])])
yields
A B C D 0 foo one 0 0 1 bar one 1 2 3 bar three 3 6 6 foo one 6 12 7 foo three 7 14

----------
Note, however, that if you wish to do this many times, it is more efficient to make an index first, and then use `df.loc`:
df = df.set_index(['B']) print(df.loc['one'])
yields
A C D B one foo 0 0 one bar 1 2 one foo 6 12
or, to include multiple values from the index use `df.index.isin`:
df.loc[df.index.isin(['one','two'])]
yields
A C D B one foo 0 0 one bar 1 2 two foo 2 4 two foo 4 8 two bar 5 10 one foo 6 12

[1]: https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o

        
Present in both answers; Present only in the new answer; Present only in the old answer;