determine values of a pandas column located at the same row as specific values of other variables

  group-by, pandas, python

Given this code, how do you print ‘res’ and ‘time_count’ corresponding ‘target’ values ?
In other words, the code should consider the location of the ‘res’ and ‘time_count’ values and print out the values of ‘target’ located in the same row.

in this code, res =

[‘time=0:1’, ‘time=2:2’, ‘time=3:1’]

which means that time=0 is the highest value for one category, time =2 is the highest value for two categories and so on. From This line, I would like to be able to print the ‘target’ value located at the same row as where time=0 is the highest value which is =3. For the second case, there are two ‘target’ values to be printed corresponding to target=1 and target=10.

time_count=
0 1
1 3
2 2
3 1

which means that time=0 occurs once, time=1 occurs 3 times etc…
I would like to print out the ‘target’ values located at the same row as the values 0,1,2,3

How can I do that?

If there is anything unclear, feel free to ask.

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,2,'A'],
    [2,5,5,1,'A'],
    [3,7,9,4,'B'],
    [1,5,1,9,'B'],
    [1,8,2,8,'C'],
    [2,8,5,10,'C'],
    [0,1,2,3,'D']]
    
print(df.iloc[0,0])

df=pd.DataFrame(data, columns=['time','x','y','target','categ'])
res = ((df.groupby('categ')['time'].max().value_counts(ascending=True).sort_index()))
res_list=([(f"time={a}:{b}") for a,b in zip(res.index, res.values)])
print(res_list,'res list')
print((res.values))

time_count=(df['time'].value_counts(ascending=True).sort_index())
print(time_count)

Source: Python Questions

LEAVE A COMMENT