Group by com Python[Nympy or Pandas] - Bring the 1st line and last line by date

Asked

Viewed 370 times

0

Good afternoon forum colleagues. I’m having a hard time bringing the 1st line and the last row grouped by date. However, I would like to bring this data in the format I am using group By in sum or Count. Follow example below.
Below follows the code I managed to make

df = pd.DataFrame({"DATA": ['28/10','28/10','29/10','29/10','29/10','30/10','30/10','30/10','31/10','31/10'],
               "MAXIMA":['21','27','25','29','24','27','28','32','29','25'],
               "MINIMA":['6','12','18','9','5','8','24','18','15','10'],
               "Close":['17','24','22','10','21','25','26','30','18','10']})
df2 = df[["DATA","MAXIMA","MINIMA"]]
df2['maxDia'] = df.groupby('DATA')["MAXIMA"].transform(np.max)
df2['minDia'] = df.groupby('DATA')["MINIMA"].transform(np.min)
df2 = df2.drop(["MAXIMA","MINIMA"], axis=1)
df2 = df2.drop_duplicates()
df2.head()  

DATA maxDia minDia
0 28/10 27 12
2 29/10 29 18
5 30/10 32 18
8 31/10 29 10

Ai put to bring the first and last, but this giving error. Is it another name? Or another way and keep the pattern of the code? Thank you very much.

df = pd.DataFrame({"DATA": ['28/10','28/10','29/10','29/10','29/10','30/10','30/10','30/10','31/10','31/10'],
           "MAXIMA":['21','27','25','29','24','27','28','32','29','25'],
           "MINIMA":['6','12','18','9','5','8','24','18','15','10'],
           "Close":['17','24','22','10','21','25','26','30','18','10']})
df2 = df[["DATA","MAXIMA","MINIMA"]]
df2['maxDia'] = df.groupby('DATA')["MAXIMA"].transform(np.max)
df2['minDia'] = df.groupby('DATA')["MINIMA"].transform(np.min)  
**df2['abeDia'] = df.groupby('DATA')["FECHAMENTO"].transform(np.first)**
**df2['fecDia'] = df.groupby('DATA')["FECHAMENTO"].transform(np.last)**
df2 = df2.drop(["MAXIMA","MINIMA"], axis=1)
df2 = df2.drop_duplicates()
df2.head()    

ERROR: Attributeerror: module 'numpy' has in attribute 'first'

  • 1

    The command first belongs to pandas, and not to numpy. Just change your np.first for "first"(as string) that will work :)

1 answer

1


The function first belongs to pandas, and not to numpy. Just change your np.first for "first"(as string) that will work :)

df2 = df[["DATA","MAXIMA","MINIMA"]]
df2['maxDia'] = df.groupby('DATA')["MAXIMA"].transform(np.max)
df2['minDia'] = df.groupby('DATA')["MINIMA"].transform(np.min)  
df2['abeDia'] = df.groupby('DATA')["Close"].transform('first')
df2['fecDia'] = df.groupby('DATA')["Close"].transform('last')
df2 = df2.drop(["MAXIMA","MINIMA"], axis=1)
df2 = df2.drop_duplicates()
df2.head() 
#saida

    DATA    maxDia  minDia  abeDia  fecDia
0   28/10   27  12  17  24
1   28/10   27  12  17  24
2   29/10   29  18  22  21
3   29/10   29  18  22  21
4   29/10   29  18  22  21
  • 1

    Sensational. Thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.