Selecting rows from pandas DataFrame using a list

Baker Johnson

I have a list of lists as below

[[1, 2], [1, 3]]

The DataFrame is similar to

  A B C
0 1 2 4
1 0 1 2
2 1 3 0

I would like a DataFrame, if the value in column A is equal to the first element of any of the nested lists and the value in column B of the corresponding row is equal to the second element of that same nested list.

Thus the resulting DataFrame should be

  A B C
0 1 2 4
2 1 3 0
Data_addict

The code below do want you need:

tmp_filter = pandas.DataFrame(None) #The dataframe you want
# Create your list and your dataframe
tmp_list = [[1, 2], [1, 3]]
tmp_df = pandas.DataFrame([[1,2,4],[0,1,2],[1,3,0]], columns = ['A','B','C'])

#This function will pass the df pass columns by columns and
#only keep the columns with the value you want
def pass_true_df(df, cond):
    for i, c in enumerate(cond):
        df = df[df.iloc[:,i] == c]
    return df

# Pass through your list and add the row you want to keep
for i in tmp_list:
    tmp_filter = pandas.concat([tmp_filter, pass_true_df(tmp_df, i)])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Selecting rows from pandas DataFrame using two columns

From Dev

Selecting multiple rows from a dataframe using a column

From Dev

Selecting rows from a Dataframe based on values in multiple columns in pandas

From Dev

Selecting rows from pandas dataframe based on cKDTree indices

From Dev

Selecting specific rows from a python dataframe for an ols regression in PANDAS

From Dev

Selecting rows from pandas dataframe based on cKDTree indices

From Dev

Selecting random rows from pandas dataframe based on counts

From Dev

python pandas selecting columns from a dataframe via a list of column names

From Dev

python pandas selecting columns from a dataframe via a list of column names

From Java

How to drop a list of rows from Pandas dataframe?

From Dev

Selecting columns from Pandas DataFrame

From Dev

Selecting columns from Pandas DataFrame

From Dev

Selecting vlaues from a dataframe in pandas

From Dev

Selecting rows with specified days in datetimeindex dataframe - Pandas

From Dev

Generate condition for selecting rows in pandas.DataFrame

From Dev

Generate condition for selecting rows in pandas.DataFrame

From Dev

Select rows of pandas dataframe from list, in order of list

From Dev

Selecting rows based on criteria from another dataframe

From Dev

Selecting Combinations of variables from DataFrame rows

From Dev

Selecting rows from pandas by subset of multiindex

From Dev

Selecting rows from an HDFStore given list of indexes

From Java

Use a list of values to select rows from a pandas dataframe

From Dev

How to generate a list of ranges to subset rows from pandas dataframe?

From Dev

Pandas: How to remove rows from a dataframe based on a list?

From Java

Selecting with complex criteria from pandas.DataFrame

From Dev

Pandas selecting discontinuous columns from a dataframe

From Dev

Memory optimization when selecting from a pandas dataframe

From Dev

Pandas: slow when selecting from dataframe

From Dev

Selecting Multiple Rows from DataGridView using Linq

Related Related

  1. 1

    Selecting rows from pandas DataFrame using two columns

  2. 2

    Selecting multiple rows from a dataframe using a column

  3. 3

    Selecting rows from a Dataframe based on values in multiple columns in pandas

  4. 4

    Selecting rows from pandas dataframe based on cKDTree indices

  5. 5

    Selecting specific rows from a python dataframe for an ols regression in PANDAS

  6. 6

    Selecting rows from pandas dataframe based on cKDTree indices

  7. 7

    Selecting random rows from pandas dataframe based on counts

  8. 8

    python pandas selecting columns from a dataframe via a list of column names

  9. 9

    python pandas selecting columns from a dataframe via a list of column names

  10. 10

    How to drop a list of rows from Pandas dataframe?

  11. 11

    Selecting columns from Pandas DataFrame

  12. 12

    Selecting columns from Pandas DataFrame

  13. 13

    Selecting vlaues from a dataframe in pandas

  14. 14

    Selecting rows with specified days in datetimeindex dataframe - Pandas

  15. 15

    Generate condition for selecting rows in pandas.DataFrame

  16. 16

    Generate condition for selecting rows in pandas.DataFrame

  17. 17

    Select rows of pandas dataframe from list, in order of list

  18. 18

    Selecting rows based on criteria from another dataframe

  19. 19

    Selecting Combinations of variables from DataFrame rows

  20. 20

    Selecting rows from pandas by subset of multiindex

  21. 21

    Selecting rows from an HDFStore given list of indexes

  22. 22

    Use a list of values to select rows from a pandas dataframe

  23. 23

    How to generate a list of ranges to subset rows from pandas dataframe?

  24. 24

    Pandas: How to remove rows from a dataframe based on a list?

  25. 25

    Selecting with complex criteria from pandas.DataFrame

  26. 26

    Pandas selecting discontinuous columns from a dataframe

  27. 27

    Memory optimization when selecting from a pandas dataframe

  28. 28

    Pandas: slow when selecting from dataframe

  29. 29

    Selecting Multiple Rows from DataGridView using Linq

HotTag

Archive