0
Good afternoon, guys. I’m messing with the Pandas library in Python and I’d like to ask you a question, if possible. I have a list of values and need to reduce this list to just one record per date. The criterion is that the third field (counter) is the largest of its date. To do this, I used the following code:
import pandas as pd
lista = [("2020-11-16", "803", 4),
("2020-11-16", "801", 18),
("2020-11-16", "802", 20),
("2020-11-17", "801", 12),
("2020-11-17", "802", 6)]
df = pd.DataFrame(lista)
df.columns=['data', 'codigo', 'contador']
df2 = df.groupby('data')['contador'].max()
for i in df2.items():
data = i[0]
contador = i[1]
print(data, contador)
#O resultado obtido foi o seguinte e está correto:
['2020-11-16', 20]
['2020-11-17', 12]
My doubt would be if it is possible to bring together in this result the column 'code', without it passing by grouping. If I put it in the groupby I will have multiple records per date and it will be wrong, because I can only have one record per date. Would there be any way to do this, whether for pandas or otherwise? Thanks from now on.
@imonferrari, beautiful solution! I’m not in the habit of using the method
isin()
, because I usually work with the whole base. It will surely make me think differently from now on.– Paulo Marques
Oops! Thank you, Paul!
isin
is a very fast method, once you start using becomes an addiction kkkk. Pranks part... Great Hug!– lmonferrari
Perfect. I didn’t know this possibility of filter with isin(). Thank you very much!
– jhenrique