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'
The command
first
belongs to pandas, and not to numpy. Just change yournp.first
for"first"
(as string) that will work :)– Terry