Home > Mobile >  Locating all numbers not present in multiple columns
Locating all numbers not present in multiple columns

Time:12-02

I am having difficulties trying to locate multiple values from columns in a csv file.

So far i have tried defining the columns from which i want to extract the values as,

Assignments = (data.loc[:, ~data.columns.isin(['A', 'B','C'])])

This should take each column not named 'A', 'B' of 'C' from the csv file.

I tried running the code,

data.loc[(data[Assignments] != 20)]

but i am met with the error message: # If we have a listlike key, _check_indexing_error will raise

KeyError: 100

The wanted outcome is a list of all rows that don't contain the value 20 (I am also not sure how to add more values than one like for example != 20,10,0.

Any help is much appreciated.

CodePudding user response:

For a single value, you can do: df[(df[:] != 20).all(axis = 1)]

For multiple values you can use numpy arrays to do elementwise boolean logic:

ar1 = np.array((df[:] != 20).all(axis = 1))
ar2 = np.array((df[:] != 30).all(axis = 1))
df[ar1 & ar2]

CodePudding user response:

To select rows in a Pandas DataFrame that do not contain a specific value, you can use the ~ operator to negate the DataFrame.isin() method. This method returns a Boolean mask indicating whether each element in the DataFrame is equal to one of the values you provide.

For example, to select rows in the DataFrame that do not contain the value 20, you can use the following code:

import pandas as pd

# Load the DataFrame from a CSV file
data = pd.read_csv('data.csv')

# Select the columns you want to include
assignments = data.loc[:, ~data.columns.isin(['A', 'B', 'C'])]

# Select rows that do not contain the value 20
selected = data.loc[~data.isin([20]).any(axis=1)]

This code first selects the columns in the DataFrame that are not named 'A', 'B', or 'C'. It then uses the isin() method to create a Boolean mask indicating which rows contain the value 20. Finally, it uses the ~ operator to negate the mask, selecting only the rows that do not contain the value 20.

To include multiple values in the isin() method, you can pass them as a list or array to the values parameter. For example, to select rows that do not contain the values 20, 10, or 0, you can use the following code:

import pandas as pd

# Load the DataFrame from a CSV file
data = pd.read_csv('data.csv')

# Select the columns you want to include
assignments = data.loc[:, ~data.columns.isin(['A', 'B', 'C'])]

# Select rows that do not contain the values 20, 10, or 0
selected = data.loc[~data.isin([20, 10, 0]).any(axis=1)]
  • Related