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, 2023 in Others by Kithuzzz
• 38,010 points
1,497 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, 2023 by narikkadan
• 63,420 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
• 63,420 points
725 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
• 63,420 points
627 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
• 63,420 points
989 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
• 63,420 points
292 views
0 votes
1 answer

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

answered Mar 20, 2023 in Others by narikkadan
• 63,420 points
376 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 4,071 views
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
• 63,420 points
424 views
0 votes
1 answer

Convert column in excel date format (DDDDD.tttt) to datetime using pandas

Given # s = df['date'] s 0 ...READ MORE

answered Oct 2, 2022 in Others by narikkadan
• 63,420 points
2,808 views
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