Given this pandas dataframe, I am trying to print the maximum value of time for each value under categ. For instance, since max time of A=6, max time of B=9 and max time of C=9, I would like to print something like time=9:2, time=6:1. How would you do it?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import xgboost as xgb
import datetime
import seaborn as sns
from sklearn.metrics import r2_score
data=[[1, 1,2 ,5,'A'],
[2, 5,5,6,'A'],
[3, 4,6,6,'A']
,[5, 6,5,6,'A'],
[7,9,9,7,'B'],
[8, 7,9,4,'B']
,[9, 2,3,8,'B'],
[2, 5,1,9,'B'],
[2,2,10,9,'C']
,[2, 8,2,8,'C'],
[9, 5,4,10,'C'],
[6, 8,5 ,10,'C']]
df = pd.DataFrame(data, columns=['time','x','y','target','categ'])
You can groupby
'categ' and apply max
to obtain the maximum per group, then count the values using value_counts
res = df.groupby('categ')['time'].max().value_counts()
[print(f"time={a}:{b}") for a,b in zip(res.index, res.values)]
time=9:2
time=5:1
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.