Slicing a Pandas Dataframe Using Two Rows

I have a large data size. I want to select data for Machine1 and for NorthAmerica. Therefore, if Machine1 and NorthAmerica are in the data series, I want to keep the row.

I know how to do this with one requirement:

df = df[df['MachineNumber'].isin(['Machine1'])]

It works fine and cuts all the data I need. However, I do not know how to do this for two things.

I tried to do this twice, separately like this:

df = df[df['MachineNumber'].isin(['Machine1'])]
df = df[df['Region'].isin(['NorthAmerica'])]

and i also tried

df = df[(df['Region']=='NorthAmerica') & (df['MachineNumber']=='Machine1')]

but both attempts throw an error TypeError: unsupported type for add operationand return an empty framework with column names. I also looked at solutions on the Internet, but they focus on the second solution, but with numbers, not lines. How can I do it right?

An example of data input is a csv called sortingdata.csv with two columns:

Region  MachineNumber
EU  Machine1
EU  Machine1
EU  Machine1
EU  Machine1
EU  Machine1
EU  Machine1
EU  Machine1
EU  Machine1
EU  Machine2
NA  Machine2
NA  Machine2
NA  Machine2
NA  Machine2
EMEA    Machine2
NA  Machine2
NA  Machine2
NA  Machine1
NA  Machine1
NA  Machine1
NA  Machine1
NA  Machine1
NA  Machine1
NA  Machine1
NA  Machine1
NA  Machine1

and code

import pandas as pd
df = pd.read_csv('sortingdata.csv') 

df = df[(df['Region']=='NorthAmerica') & (df['MachineNumber']=='Machine1')]

, Empty DataFrame.

0
1

, , .

# data
# ==================================
df

   Region MachineNumber
0      EU      Machine1
1      EU      Machine1
2      EU      Machine1
3      EU      Machine1
4      EU      Machine1
5      EU      Machine1
6      EU      Machine1
7      EU      Machine1
..    ...           ...
17     NA      Machine1
18     NA      Machine1
19     NA      Machine1
20     NA      Machine1
21     NA      Machine1
22     NA      Machine1
23     NA      Machine1
24     NA      Machine1

[25 rows x 2 columns]

# processing
# ===============================
df[(df['Region']=='NA') & (df['MachineNumber']=='Machine1')]

   Region MachineNumber
16     NA      Machine1
17     NA      Machine1
18     NA      Machine1
19     NA      Machine1
20     NA      Machine1
21     NA      Machine1
22     NA      Machine1
23     NA      Machine1
24     NA      Machine1
+1

Source: https://habr.com/ru/post/1598789/


All Articles