Excel formula in Pandas

0 votes

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:

enter image description here

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 in Others by Kithuzzz
• 28,900 points
60 views

1 answer to this question.

0 votes

 Use this:

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

I hope this helps you.

answered Jan 6 by narikkadan
• 53,520 points

Related Questions In Others

0 votes
1 answer

Moving Average formula in Excel not autofilling in table

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

answered Oct 2, 2022 in Others by narikkadan
• 53,520 points
140 views
0 votes
1 answer

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

answered Oct 8, 2022 in Others by narikkadan
• 53,520 points
164 views
0 votes
1 answer

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

answered Oct 10, 2022 in Others by narikkadan
• 53,520 points
208 views
0 votes
1 answer

Shortcut to Apply a Formula to an Entire Column in Excel

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

answered Oct 29, 2022 in Others by narikkadan
• 53,520 points
83 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

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

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,329 views
0 votes
1 answer
0 votes
1 answer

Is there a maximum number of formula fields allowed in Excel (2010)

See http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx for limits on specs it doesn't indicate ...READ MORE

answered Sep 30, 2022 in Others by narikkadan
• 53,520 points
99 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP