What i need to find is for all my keys values what is the 3rd quartile? then I would need to display that information in some way for each Key. below is an example of what im looking for but the 2nd dataframe can look different
Dataframe A -> Dataframe A
Key, value key, value, Quartile(3rd)
A 2 A 2 result of third quartile here X as placeholder
B 3 B 3 result of third quartile here Y as placeholder
A 4 A 4 x
A 5 A 5 x
A 6 A 6 x
B 6 B 6 y
C 1 C 6 z
etc
The quartile doesn't need to be inserted into a new column i just need to know for all my A values what is the 3rd Quartile.
CodePudding user response:
You can use GroupBy.quantile
with 0.75 for the 75% quantile (3rd quartile):
df.groupby('Key')['value'].quantile(0.75)
output:
Key
A 5.25
B 5.25
C 1.00
Name: value, dtype: float64
To repeat the values for all rows per group you can use transform
:
df['Quartile(3rd)'] = df.groupby('Key')['value'].transform(lambda s: s.quantile(0.75))
output:
Key value Quartile(3rd)
0 A 2 5.25
1 B 3 5.25
2 A 4 5.25
3 A 5 5.25
4 A 6 5.25
5 B 6 5.25
6 C 1 1.00