-3

I am trying to match this data column to column. I want to match first cell of column 1 to first cell of Column 3, then second cell of column 1 to second cell of column 3 and so on. My requirement is to get the result in Boolean. I am a novice and working on python for a week now.

My data is 1800 rows and 35 columns. What I am trying to do is simply use =(A2=C2) of excel but into python.

Please provide the exact code for the given data frame.

Thanks in advance

Data Set

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
Maneet Giri
  • 185
  • 3
  • 18
  • are you after `df[[2]] == df[[0]]`? – MaxU - stand with Ukraine Apr 13 '16 at 19:45
  • I tried (1,Column1)=(1,Column3) and similar – Maneet Giri Apr 13 '16 at 19:45
  • Maxu - Yes, but I am not sure how will i loop it for whole column and that too cell by cell – Maneet Giri Apr 13 '16 at 19:46
  • Advice: Put the code you have an issue with in your question. Insert it in the style of a short unit test. This makes it easy for someone to copy and paste it straight into `ipython` and work with your issue. – Alexander McFarlane Apr 13 '16 at 20:33
  • possibly a duplicate of http://stackoverflow.com/questions/16096627/pandas-select-row-of-data-frame-by-integer-index – Alexander McFarlane Apr 13 '16 at 20:35
  • This questions doesn't show signs of effort. You should show what have you tried so far. It seems to me that you want that we solve your problem. – Alejandro Apr 13 '16 at 21:25
  • Alejandro - Well, as I mentioned I am a novice and as for my effort. I was trying to solve this for last three days. I tried list to list match and also and tried to match one cell to another by using (1,Column1)=(1,Column3) . apologies if you felt that way. It was first ever question I asked and joined stackoverflow.com in seach of my answer. I will be more specific with queries I tried and efforts made from next time. – Maneet Giri Apr 14 '16 at 02:07

2 Answers2

0

When you compare two Numpy arrays of the same dimensions, you get an array of booleans - one for each array elements.

So if a = np.array([1,2,3,4,5]) b = np.array([1,3,2,5,5])

You get

a==b  ==> [True, False, False, False, True] 
zmbq
  • 38,013
  • 14
  • 101
  • 171
0

try this:

In [28]: df
Out[28]:
   a  b  c
0  5  1  2
1  5  2  8
2  3  3  8
3  3  1  4
4  6  3  4
5  1  0  0
6  6  0  0
7  4  4  3
8  3  0  4
9  9  0  9

In [29]: df.iloc[:, 0] == df.iloc[:, 2]
Out[29]:
0    False
1    False
2    False
3    False
4    False
5    False
6    False
7    False
8    False
9     True
dtype: bool

if you need a new boolean column:

In [30]: df['bool'] = df.iloc[:, 0] == df.iloc[:, 2]

In [31]: df
Out[31]:
   a  b  c   bool
0  5  1  2  False
1  5  2  8  False
2  3  3  8  False
3  3  1  4  False
4  6  3  4  False
5  1  0  0  False
6  6  0  0  False
7  4  4  3  False
8  3  0  4  False
9  9  0  9   True
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419