Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slice pandas DataFrame by disjunction statement (logical "or")? [duplicate]

I would like to slice a pandas.DataFrame which satisfies condition A or condition B. Most of the search results only show how to slice dataframe using "and". So I wonder if it is possible to use "or" operator without converting (A and B) to (not (not A and not B))? Because sometimes there are many "or" conditions needed, and converting might be troublesome.

I tried to use:

df[(df['c1']==x1) or (df['c2']==x2)]

but it does not work.

like image 681
user18243nanana Avatar asked Aug 16 '16 01:08

user18243nanana


People also ask

Which operator is used to slice the rows in DataFrame?

iloc method. For now, we explain the semantics of slicing using the [] operator. With DataFrame, slicing inside of [] slices the rows. This is provided largely as a convenience since it is such a common operation.

How do you slice series Pandas?

slice() method is used to slice substrings from a string present in Pandas series object. It is very similar to Python's basic principal of slicing objects that works on [start:stop:step] which means it requires three parameters, where to start, where to end and how much elements to skip.

What is slicing in Pandas?

Slicing using the [] operator selects a set of rows and/or columns from a DataFrame. To slice out a set of rows, you use the following syntax: data[start:stop] . When slicing in pandas the start bound is included in the output. The stop bound is one step BEYOND the row you want to select.

How to slice columns in pandas Dataframe?

How to Slice Columns in pandas DataFrame 1 Quick Examples of Column-Slices of Pandas DataFrame#N#If you are in a hurry, below are some quick examples of how to... 2 Pandas DataFrame.iloc [] – Column Slices by Index or Position#N#By using pandas.DataFrame.iloc [] you can slice... 3 Complete Example To Take Column-Slices From DataFrame More ...

How to manipulate pandas Dataframe in Python?

Case 3: Manipulating Pandas Data frame. Manipulation of the data frame can be done in multiple ways like applying functions, changing a data type of columns, splitting, adding rows and columns to a data frame, etc. Example 1: Applying lambda function to a column using Dataframe.assign () Python3. import pandas as pd.

How to slice a Dataframe using indexing in Python?

Slicing of a DataFrame can be done using the following two methods: Using loc, the loc is present in the pandas package loc can be used to slice a dataframe using indexing. Pandas DataFrame.loc attribute access a group of rows and columns by label (s) or a boolean array in the given DataFrame.

How to divide a pandas Dataframe into two different subsets?

This example explains how to divide a pandas DataFrame into two different subsets that are split at a particular row index. For this, we first have to define the index location at which we want to slice our data set (i.e. 3): In the next step, we can use this splitting point to extract all rows before this index point:


1 Answers

You need to use the logical or symbol |

df[(df['c1'] == x1) | (df['c2'] == x2)]

For and, you would need to use &

df[(df['c1'] == x1) & (df['c2'] == x2)]
like image 123
Alexander Avatar answered Oct 19 '22 22:10

Alexander