Based on the values in the first column, I'm attempting to construct a column with zeros and ones. Write 1 if the value of the upper cell is greater; else, write 0. An example of code might be as follows:

```df = pd.Dataframe({'col1': [1, 2, 1, 3, 0]})
df['col2'] =  ...python version of excel formula IF(A2>A3, 1, 0)...
```

Expected output:

I have tried:

```while True:
for index, rows in df.iterrows():
df['col1'] = np.where(df['col1'] > df['col1'][index+1], 1, 0)
```

But this is very slow and gives wrong results.

Jan 6, 2023 in Others 1,634 views

## 1 answer to this question.

Use this:

`df['col2'] = df['col1'].shift().lt(df['col1']).astype(int)`

I hope this helps you.

• 63,700 points

## Moving Average formula in Excel not autofilling in table

You could use AVERAGEIFS to make the ...READ MORE

## INDEX formula in Excel, Top 10, repeats previous value

Try this formula in cell W4: =IF(V3=V4,INDEX(INDIRECT("I"&MATCH(W3,I:I,0)+1&":I26"),MATCH(V4,INDIRECT("R"&MATCH(W3,I:I,0)+1&":R26"),0)),INDEX(\$I\$2:\$I\$26,MATCH(V4,\$R\$2:\$R\$26,0))) The calculation ...READ MORE

## Excel formula for searching two text in one cell and return values based on the result

You can include a second IF within ...READ MORE

## Shortcut to Apply a Formula to an Entire Column in Excel

Try double-clicking on the bottom right hand ...READ MORE

## Reading Excel file Date/time Incorrectly

Use dateutil.parser. Example usage: import dateutil.parser as parser parser.parse("2021-04-04 00:00:00 1900-01-01 ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE