简体   繁体   中英

comparing column values in groupby in pandas

my dataframe look like this

Time    Name    price   Profit   
5:25    A        150       15
5:25    B        250       10
5:25    C        200       20
5:30    A        200       25
5:30    B        150       20
5:30    C        210       25
5:35    A        180       15
5:35    B        200       30
5:35    C        200       10 
5:40    A        150       20 
5:40    B        260       15 
5:40    C        220       10   

I want output should be like:

Time    Name    price  profit    diff_price   diff_profit      
5:25    A        150     15         0            0
5:25    B        250     10         0            0
5:25    C        200     20         0            0
5:30    A        200     25        50            10
5:30    B        150     20        -100          10
5:30    C        210     25         10            5
5:35    A        180     15         20            -10
5:35    B        200     30         50            10
5:35    C        200     10         -10           -15
5:40    A        150     20         -30           5
5:40    B        260     35          60           5
5:40    C        220     15          20           5

I need to compare between previous values of groupby is greater than of previous values like

difference of A,B and C are greater than previous values or not. if condition matches it has to display Name:

from above at Time 5:40, diff_price and diff_profit of B is greater than all previous Time column values

so output should print like: B

my code look like

df.groupby(['Time','Price'])
df['diff_price']=df.groupby(['Time','Price']).price.diff().fillna(0)
df['diff_profit']=df.groupby(['Time','Price']).profit.diff().fillna(0)

Then how to do comparision between values to get desired output to display is: B

IIUC, compute diff_price and diff_profit based on Name column then patch the last group of time according your condition:

df[['diff_price', 'diff_profit']] = df.groupby('Name')[['price', 'profit']] \
                                      .diff().fillna(0)

mask = df['Time'].eq(df['Time'].max())
df.loc[mask, 'diff_profit'] = df.loc[mask, 'diff_profit'].max()

Output:

>>> df
    Time Name  price  profit  diff_price  diff_profit
0   5:25    A    150      15         0.0          0.0
1   5:25    B    250      10         0.0          0.0
2   5:25    C    200      20         0.0          0.0
3   5:30    A    200      25        50.0         10.0
4   5:30    B    150      20      -100.0         10.0
5   5:30    C    210      25        10.0          5.0
6   5:35    A    180      15       -20.0        -10.0
7   5:35    B    200      30        50.0         10.0
8   5:35    C    200      10       -10.0        -15.0
9   5:40    A    150      20       -30.0          5.0
10  5:40    B    260      15        60.0          5.0
11  5:40    C    220      10        20.0          5.0

You could tackle this problem one group ("Name") at the time:

# Let's iterate the dataframe by grouping by "Name"
for name, group_df in df.groupby(["Name"]):
    # Make sure that the rows are sorted by time
    group_df = group_df.sort_values("Time")
    # Calculate difference between each row (diff = bottom - top)
    group_df[["diff_price", "diff_profit"]] = group_df[["price", "Profit"]].shift(1) - group_df[["price", "Profit"]]
    # Fill the first value with 0 instead of NaN (as in your sample input)
    group_df = group_df.fillna(0)

    # Let's see if the maximum diff_price is reached at the end
    *previous_values, last_value = group_df["diff_price"]
    if last_value >= max(previous_values):
        print(f"Max price diff reached at '{name}'")
        print(group_df.tail(1))

    # Again, but let's checkout the diff_profit
    *previous_values, last_value = group_df["diff_profit"]
    if last_value >= max(previous_values):
        print(f"Max profit diff reached at '{name}'")
        print(group_df.tail(1))

This is the output I get for your sample input:

Max price diff reached: A
   Time Name  price  Profit  diff_price  diff_profit
9  5:40    A    150      20        30.0         -5.0
Max profit diff reached: B
    Time Name  price  Profit  diff_price  diff_profit
10  5:40    B    260      15       -60.0         15.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM