简体   繁体   中英

Drop rows based on conditions and create new columns of pandas dataframe

I have this pandas dataframe:

      Trade #     Segnale          Data/Ora  Prezzo
0        1        Long  2022-01-12 14:00   43302
1        1   Exit Long  2022-01-12 22:00   44169
2        2       Short  2022-01-14 12:00   42093
3        2  Exit short  2022-01-14 15:00   42514

You can reproduce with:

tbl2 = {"Trade #" :[1,1,2,2],
       "Segnale" : ["Long", "Exit Long", "Short", "Exit short"],
       "Data/Ora" : ["2022-01-12 14:00", "2022-01-12 22:00", "2022-01-14 12:00",
                     "2022-01-14 15:00"],
        "Prezzo" : [43302, 44169, 42093, 42514]}

df = pd.DataFrame(tbl2)

My goal is to drop the rows if "Segnale" column has ("Exit Long" OR "Exit short") as row take the "Data/Ora" and "Prezzo" row and create two columns with that values names Data/Ora_exit and Prezzo_exit,

This is how it should look like the final dataframe:

df2

 Trade #     Segnale             Data/Ora   Prezzo   Data/Ora_exit            Prezzo_exit
       1    Long          2022-01-12 14:00  43302    2022-01-12 22:00            44169
         
       2    Short          2022-01-14 12:00 42093     2022-01-14 15:00           42514

Any ideas?

Try this,

df = df.groupby('Trade #').agg({'Data/Ora': ['min', 'max'], 'Prezzo': ['min', 'max'], 'Segnale': 'first'})
df.columns = ["_".join(x) for x in df.columns]
df = df.reset_index()

O/P:

   Trade #      Data/Ora_min      Data/Ora_max  Prezzo_min  Prezzo_max  \
0        1  2022-01-12 14:00  2022-01-12 22:00       43302       44169   
1        2  2022-01-14 12:00  2022-01-14 15:00       42093       42514   

  Segnale_first  
0          Long  
1         Short 

Note: Rename/Reorder columns as you desire

here is one way to do it

m=df[(df['Segnale'].isin(['Exit Long', 'Exit short']))].add_suffix('_exit')
pd.concat([df, m.iloc[:,2:]], axis=1).groupby('Trade').first()

OR

m=df.mask(df['Segnale'].isin(['Exit Long', 'Exit short'])).add_suffix('_exit')
(pd.concat([df,m.iloc[:,2:]], axis=1)).dropna()

    Segnale     Data/Ora    Prezzo  Data/Ora_exit   Prezzo_exit
Trade                   
1   Long    2022-01-12 14:00    43302   2022-01-12 22:00    44169.0
2   Short   2022-01-14 12:00    42093   2022-01-14 15:00    42514.0

df.iloc[::2, :].merge( df.iloc[1::2, :].rename(columns={'Data/Ora': 'Data/Ora exit', 'Prezzo': 'Prezzo exit'}).drop('Segnale', axis=1), on='Trade #' )

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