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, 2020 in Python by moli
• 140 points
73 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, 2020 by Niroj
• 82,760 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
221 views
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,910 points
719 views
0 votes
0 answers

i want to ask that how can i run one file of python in another file in jupyter notebook

motion_detection.ipynb # Python program to implement # Webcam ...READ MORE

Dec 15, 2020 in Python by Ramsha
• 120 points
152 views
+1 vote
3 answers

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
69,442 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 6, 2019 in Python by Neha
• 330 points

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

How do I make a field readonly (or disabled) so that it cannot be edited in Django form?

Hello @kartik, Setting readonly on a widget only makes the ...READ MORE

answered Aug 6, 2020 in Python by Niroj
• 82,760 points
5,098 views
+1 vote
3 answers

How to change/update cell value in Python Pandas dataframe?

You can use the at() method to ...READ MORE

answered Apr 8, 2019 in Python by Kunal
119,911 views