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

Question:

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?

Asked By: Pren Ven

||

Answers:

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

Answered By: Mohamed Thasin ah

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

Answered By: Naveed
df.iloc[::2, :].merge(
    df.iloc[1::2, :].rename(columns={'Data/Ora': 'Data/Ora exit', 'Prezzo': 'Prezzo exit'}).drop('Segnale', axis=1), on='Trade #'
)
Answered By: Confused Learner
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.