I am trying to add quantiles based on column J1 within each ID group in the below dataframe.
import pandas as pd
try_df = pd.DataFrame({'ID':['1','1','1','1','1','2','2','2','2','2','3','3','3','3','3'], 'J1': range(15)})
print(try_df)
try_df["quantiles"] = try_df.groupby("ID")["J1"].transform(pd.qcut,4,["Q1","Q2","Q3","Q4"])
ID J1
0 1 0
1 1 1
2 1 2
3 1 3
4 1 4
5 2 5
6 2 6
7 2 7
8 2 8
9 2 9
10 3 10
11 3 11
12 3 12
13 3 13
14 3 14
The above code gives a value error: invalid literal for long() with base 10: 'Q4'
In the real data it throws this error:
ValueError: could not convert string to float: Q2
Any suggestions on how to resolve it?