Python: logical comparing with columns in panda's dataframe

dustin

I have a dataframe where I want to determine when the ser_no and CTRY_NM are the same and differ. However, I want to be mindful of the ser_no changes and not make a false and false return true or a false/true return false.

Consider the following dataframe:

import pandas as pd
df = pd.DataFrame({'ser_no': [1, 1, 1, 2, 2, 2, 2, 3, 3, 3],
                'CTRY_NM': ['a', 'a', 'b', 'e', 'e', 'a', 'b', 'b', 'b', 'd']})
def check(key):
    return df[key] == df[key].shift(1)

match = check('ser_no') == check('CTRY_NM')

This returns:

enter image description here

However, at indices, 4 and 8 we have serial number changes. Since each serial number is a different machine, it doesn't make sense to have a logical comparison at these locations. When ser_no changes, how can I insert NaN instead of do a logical comparison?

cncggvg

is this what you want?

def check(data, key):
    mask = data[key].shift(1) == data[key]
    mask.iloc[0] = np.nan
    return mask

df.groupby(by=['ser_no']).apply(lambda x: check(x, 'CTRY_NM'))

result

ser_no   
1       0   NaN
        1     1
        2     0
2       3   NaN
        4     1
        5     0
        6     0
3       7   NaN
        8     1
        9     0
Name: CTRY_NM, dtype: float64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

creating a logical panda series by comparing two series

From Dev

Panda's equivalent of R's order() for arranging dataframe columns

From Dev

Logical OR on a subset of columns in a DataFrame

From Dev

Logical OR on a subset of columns in a DataFrame

From Dev

Panda's DataFrame - renaming multiple identically named columns

From Dev

mean of all the columns of a panda dataframe?

From Dev

Converting list in panda dataframe into columns

From Dev

Finding the common columns when comparing two rows in a dataframe in python

From Dev

Logical operation on two columns of a dataframe

From Dev

Scala Comparing time columns in dataframe

From Dev

comparing columns pandas python

From Dev

Creating a new column in panda dataframe using logical indexing and group by

From Dev

Pandas dataframe If else with logical AND involving two columns

From Dev

Apply a function in a dataframe's columns [Python]

From Dev

Filtering a pandas dataframe comparing two columns

From Dev

Python comparing variables in 2 dataframe

From Dev

Comparing two dataframe values in python

From Dev

Python Panda count occurences depending on multiple columns

From Dev

Python Panda count occurences depending on multiple columns

From Dev

Delete rows from python panda dataframe

From Java

Iterate through multiple columns in a Panda dataframe and find count unique values

From Dev

Make new column in Panda dataframe by adding values from other columns

From Dev

How to group columns by label in a histogram using a panda DataFrame?

From Dev

Using panda for comparing column values and creating column based on the values in compared columns?

From Java

Groupby based on a multiple logical conditions applicated to a different columns DataFrame

From Dev

Can't remove columns from a dataframe, output turns into a logical vector

From Dev

Comparing two dataframe (python pandas) by datetime intervals

From Dev

pandas: get rows by comparing two columns of dataframe to list of tuples

From Dev

Filling NaNs with values from column of another dataframe by comparing the columns

Related Related

  1. 1

    creating a logical panda series by comparing two series

  2. 2

    Panda's equivalent of R's order() for arranging dataframe columns

  3. 3

    Logical OR on a subset of columns in a DataFrame

  4. 4

    Logical OR on a subset of columns in a DataFrame

  5. 5

    Panda's DataFrame - renaming multiple identically named columns

  6. 6

    mean of all the columns of a panda dataframe?

  7. 7

    Converting list in panda dataframe into columns

  8. 8

    Finding the common columns when comparing two rows in a dataframe in python

  9. 9

    Logical operation on two columns of a dataframe

  10. 10

    Scala Comparing time columns in dataframe

  11. 11

    comparing columns pandas python

  12. 12

    Creating a new column in panda dataframe using logical indexing and group by

  13. 13

    Pandas dataframe If else with logical AND involving two columns

  14. 14

    Apply a function in a dataframe's columns [Python]

  15. 15

    Filtering a pandas dataframe comparing two columns

  16. 16

    Python comparing variables in 2 dataframe

  17. 17

    Comparing two dataframe values in python

  18. 18

    Python Panda count occurences depending on multiple columns

  19. 19

    Python Panda count occurences depending on multiple columns

  20. 20

    Delete rows from python panda dataframe

  21. 21

    Iterate through multiple columns in a Panda dataframe and find count unique values

  22. 22

    Make new column in Panda dataframe by adding values from other columns

  23. 23

    How to group columns by label in a histogram using a panda DataFrame?

  24. 24

    Using panda for comparing column values and creating column based on the values in compared columns?

  25. 25

    Groupby based on a multiple logical conditions applicated to a different columns DataFrame

  26. 26

    Can't remove columns from a dataframe, output turns into a logical vector

  27. 27

    Comparing two dataframe (python pandas) by datetime intervals

  28. 28

    pandas: get rows by comparing two columns of dataframe to list of tuples

  29. 29

    Filling NaNs with values from column of another dataframe by comparing the columns

HotTag

Archive