Interesting question!
Based on Scott's suggestions I gave it a quick try:
import random
import pandas as pd
import numpy as np
# given inputs
count = 5388
mean = 4173
median = 4072
Q1 = 2720
Q9 = 5676
# fixing the random seed
random.seed(a=1, version=2)
# creating parts of the dataset
a = []
b = []
c = []
d = []
# 1st Decile
for i in range(0,539):
i = random.randint(2101, 2720)
a.append(i)
a.sort()
# 9th Decile
for i in range(0,538):
i = random.randint(5676, 6325)
b.append(i)
b.sort()
# from 1st decile to median
for i in range(0,2155):
i = random.randint(2720, 4072)
c.append(i)
c.sort()
# can be used to adjust the mean
mean_adjuster = 5790
# from median to 9th decile
for i in range(0,2156):
i = random.randint(4072, mean_adjuster)
d.append(i)
d.sort()
# combining lists
dataset = []
dataset = a + b + c + d
# converting into DataFrame
df = pd.DataFrame({"x": dataset})
count_new = len(df)
mean_new = np.mean(df.x)
median_new = np.quantile(df.x, 0.5)
Q1_new = np.quantile(df.x, 0.1)
Q9_new = np.quantile(df.x, 0.9)
# formating floats
pd.options.display.float_format = '{:.1f}'.format
compare = pd.DataFrame({"value": ["count", "mean", "median", "Q1", "Q9"],
"original": [count, mean, median, Q1, Q9],
"new":[count_new, mean_new,median_new, Q1_new, Q9_new ]})
print(compare)
Output:
value original new
0 count 5388 5388.0
1 mean 4173 4173.4
2 median 4072 4072.5
3 Q1 2720 2720.4
4 Q9 5676 5743.0
Getting the values to be exactly equal is a bit tricky when all your values are integers and not floats..
You can add another variable to control the mean with two numbers or change the random seed and see if you can get a closer values. Alternatively, you can write a function that changes the seed until the values are equal. (might take couple of minutes or couple of centuries:)
Cheers!