How can I optimize this condition so that it takes less time to execute in Python Pandas.

0 votes

Hello, I have a dataframe that contain some data.

Purchased Price        Selling Price
100                    200
100                    99
200                    150
100                    150
50                     60
70                     50
80                     100
25                     15
30                     40
50                     25

I want to optimize this program so that it takes less time.
program code is:-

import pandas as pd
data_store = pd.read_excel("101010.xlsx")

if data_store.iloc[0]["Purchased Price"] > data_store.iloc[0]["Selling Price"]:
    # Execute some program.
    print(data_store.iloc[0], "\nTaking Loss")
else:
    # Execute some program.
    print(data_store.iloc[0], "\nTaking Profit")

if data_store.iloc[1]["Purchased Price"] > data_store.iloc[1]["Selling Price"]:
    # Execute some program.
    print(data_store.iloc[1], "\nTaking Loss")
else:
    # Execute some program.
    print(data_store.iloc[1], "\nTaking Profit")

if data_store.iloc[2]["Purchased Price"] > data_store.iloc[2]["Selling Price"]:
    # Execute some program.
    print(data_store.iloc[2], "\nTaking Loss")
else:
    # Execute some program.
    print(data_store.iloc[2], "\nTaking Profit")

Please can any body tell me that how can I optimize this programme so that it takes less time to execute.
Thank you in advance :) :) :)


Sep 8 in Python by moli
• 140 points
31 views

1 answer to this question.

0 votes

Hello @moli,

You can easily optimize this code by defining a function.and add for loop.

Here is the solution:

import pandas as pd
data_store = pd.read_excel("101010.xlsx")

for i in range(3):

    if data_store.iloc[i]["Purchased Price"] > data_store.iloc[i]["Selling Price"]:
      # Execute some program.
       print(data_store.iloc[i], "\nTaking Loss")
    else:
      # Execute some program.
       print(data_store.iloc[i], "\nTaking Profit")

Hope it helps!!

Thank you!!

answered Sep 8 by Niroj
• 66,420 points

Related Questions In Python

0 votes
1 answer

How can I raise an exception in Python so that it can later be caught via an except block?

It's pretty simple to raise a query  raise ...READ MORE

answered May 29, 2019 in Python by Umesh
149 views
0 votes
1 answer
+1 vote
1 answer

How can I use python to execute a curl command?

For sake of simplicity, maybe you should ...READ MORE

answered Oct 11, 2018 in Python by charlie_brown
• 7,780 points
54,989 views
0 votes
1 answer

How can I prevent or alter access to class variables in Python?

The ActiveState solution that Pynt references makes instances of ...READ MORE

answered Dec 5, 2018 in Python by aryya
• 7,380 points
144 views
+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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,454 views
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

How can I disable logging while running unit tests in Python Django?

Hello @kartik, Since you are in Django, you ...READ MORE

answered Jun 23 in Python by Niroj
• 66,420 points
958 views