How to write this condition in one line and run this

0 votes

Hello, I have a dataframe that contain some data. I want to apply a condition on this dataframe.

When i wrote the code like this:-

import pandas as pd 
data_store = pd.read_excel("101010.xlsx") 
fff = data_store[data_store.Price > 20] 
print(fff.sort_values("Total Sale", ascending=False))

The program is running and return(show) dataframe elements value as i want like this:-

Total Sale      Price
100000           64
95000            21
94000            25
70000            30
61000            41
59000            23
54000            50
40000            55

But when i wrote that code in single line like this:-

import pandas as pd
data_store = pd.read_excel("101010.xlsx")
fff = (data_store[data_store.Price > 20]) and (data_store.sort_values("Total Sale", ascending=False))
print(fff)

I get error like this:-

Traceback (most recent call last):
  File "C:/Users/Moli/PycharmProjects/first/main.py", line 3, in <module>
    fff = (data_store[data_store.Price > 20]) and (data_store.sort_values("Total Sale", ascending=False))
  File "C:\Python37\lib\site-packages\pandas\core\generic.py", line 1327, in __nonzero__
    f"The truth value of a {type(self).__name__} is ambiguous. "
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Process finished with exit code 1

Please can some body help me how i can write this condition in single line like this:-

(data_store[data_store.Price > 20]) and (data_store.sort_values("Total Sale", ascending=False))

Thank you in advance :) :) :)


Sep 7, 2020 in Python by moli
• 140 points
450 views

Hi, @Moll,

use | instead of and.

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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