1

I am new Python Programmer. I want to solve this problem,

series1 = pd.Series([1,2,3,4,5])
series2 = pd.Series([1,3,3,4,6])

data1 = pd.DataFrame([series1,series2])
data_frame = pd.DataFrame(index=[], columns=['column1', 'column2'])
data_frame['column1'] = series1
data_frame['column2'] = series2

There are two columns, column1, column2. And I want to judge from these two columns to see

I tried these code below. But, It doesn't work. Please give me the advice?

judgeList = []

def judgeBool(x):
    judgeList.append(x)

if len(judgeList) == 2:  
    if judgeList[0] == judgeList[1]:
        series = pd.Series([True])
        data_frame = data_frame.append(series, ignore_index = True)
    else:
        series = pd.Series([False])
        data_frame = data_frame.append(series, ignore_index = True)


data_frame.apply(lambda x: judgeBool(x), axis = 1)

enter image description here

YOSUKE
  • 331
  • 3
  • 13

1 Answers1

2

First, try avoid apply, because slow.

I think need assign compared columns to new column:

data_frame['bool'] = data_frame['column1'] == data_frame['column2']
print (data_frame)
   column1  column2   bool
0        1        1   True
1        2        3  False
2        3        3   True
3        4        4   True
4        5        6  False
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252