Home > Back-end >  localizing rows from a dataframe
localizing rows from a dataframe

Time:12-22

I working with a dataframe which has 20 columns but I'm only going to use three of them in my task, which are named "Price","Retail" and "Profit" and are like this:

cols = ['Price', 'Retail','Profit']
df3.loc[:,cols]
    Price   Retail  Profit
0   861.5   1315.233051 453.733051
1   901.5   1315.233051 413.733051
2   911.0   1315.233051 404.233051
3   901.5   1315.233051 413.733051
4   901.5   1315.233051 413.733051
... ... ... ...
2678    14574.0 21546.730769    6972.730769
2679    35708.5 52026.764706    16318.264706
2680    35708.5 52026.764706    16318.264706
2681    163276.5    250882.500000   87606.000000
2682    7369.5  11785.729730    4416.229730
2683 rows × 3 columns

My goal is to find the lines where the prices are lower than 5000 and sort by the biggest values of profit. How can I make it?

CodePudding user response:

You for example use query and sort_values

df3.query("Price < 5000").sort_values("Profit", ascending=False)

CodePudding user response:

You can do this:

df_pofit_less_5000 = df[df['Price']<5000]

CodePudding user response:

You can start by subsetting the dataframe where price is lower than 5000:

df = df[df["Price"] < 5000]

Then sort by Profit descending:

df = df.sort_values(by = ["Profit"], ascending=False)
  • Related