Question: I have a simple code,

```
            Monday = Label(master, text="Subject")
            Monday.grid(row=1, column=0)
            Monday = Label(master, text="Monday")
            Monday.grid(row=1, column=2)
            Tuesday = Label(master, text="Tuesday")
            Tuesday.grid(row=1, column=3)
            Wednesday = Label(master, text="Wednesday")
            Wednesday.grid(row=1, column=4)
            Thursday = Label(master, text="Thursday")
            Thursday.grid(row=1, column=5)
            Friday = Label(master, text="Friday")
            Friday.grid(row=1, column=6)

```

[![enter image description here](https://i.stack.imgur.com/rJ5aA.png)](https://i.stack.imgur.com/rJ5aA.png)

Is there a quick way I can select all of these and move all the days into the middle of the window? I understand I could simply work out the column number for each individual day but surely there is an easier way while also keeping them perfectly spaced out like they are now.

Answer: 
You can use [DataFrameGroupBy.sample](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.DataFrameGroupBy.sample.html) to sample rows in each category.

```
n = 0.2  # 20% per category
# Sample dataframe
df = pandas.DataFrame({
        'image_id': [1,2,3,4,5,6,7],
        'product_category': ['A', 'A', 'A', 'A', 'A', 'B', 'B']
})

df.groupby('product_category').sample(frac=n)

```

However, please note that some category may return no rows if their sampled count falls below 1.
You can sample each group with a lambda as follows:

```
import pandas as pd
df = pd.read_csv("example.csv")
df.columns = ["image", "product"]

grouped = df.groupby("product")
result = grouped.apply(lambda x: x.sample(frac=0.2))

```