Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare values in two columns of data frame

Tags:

python

pandas

I have the following two columns in pandas data frame

     256   Z
0     2    2
1     2    3
2     4    4
3     4    9

There are around 1594 rows. '256' and 'Z' are column headers whereas 0,1,2,3,4 are row numbers (1st column above). I want to print row numbers where value in Column '256' is not equal to values in column 'Z'. Thus output in the above case will be 1, 3. How can this comparison be made in pandas? I will be very grateful for help. Thanks.

like image 282
Ashok K Harnal Avatar asked Jan 24 '15 09:01

Ashok K Harnal


People also ask

How do I match two column values in pandas?

To find the positions of two matching columns, we first initialize a pandas dataframe with two columns of city names. Then we use where() of numpy to compare the values of two columns. This returns an array that represents the indices where the two columns have the same value.

How do I compare two columns and return values in third column in Python?

In the Formula Type drop down list, please select Lookup option; Then, select Look for a value in list option in the Choose a formula list box; And then, in the Arguments input text boxes, select the data range, criteria cell and column you want to return matched value from separately.


1 Answers

Create the data frame:

import pandas as pd
df = pd.DataFrame({"256":[2,2,4,4], "Z": [2,3,4,9]})

ouput:

    256 Z
0   2   2
1   2   3
2   4   4
3   4   9

After subsetting your data frame, use the index to get the id of rows in the subset:

row_ids = df[df["256"] != df.Z].index

gives

Int64Index([1, 3], dtype='int64')
like image 139
cel Avatar answered Oct 06 '22 01:10

cel