This question has probably been answered a million times; and yet it is still difficult to begin working with pandas and to get used to how the dataframe works.
Thus, if someone could propose a better version of my method to retrieve the data of interest from the dataframe, I would be much obliged.
I have a dataframe which stores scores, which can be simulated with:
import random
import pandas as pd
columns = ['ID', 'Number', 'Score 1', 'Score 2', 'Score 3', 'Score 4']
df = pd.DataFrame(columns=columns)
for k in range(100):
idx = random.choice([10, 11, 12, 13, 14])
number = random.choice([1, 2, 3, 4])
scores = [random.choice(range(1000)) for _ in range (4)]
entry = pd.DataFrame([[idx, number] + scores], columns=df.columns)
df = df.append(entry)
The trick is that for a given ID, a given number N can appear X times, with X non-constant. However, for each line in the dataframe, the number of scores is fixed, i.e. in this example 4.
I want to select the score 4 for a given ID and group them by number. e.g.
# Retrieve scores of ID 10
id_scores = df.loc[df['ID'] == 10]
numbers = id_scores.Number.unique()
data = list()
for n in numbers:
data.append(id_scores.loc[id_scores['Number'] == n]['Score 4'].values)
The code above works.. but I don't need to be proficient with pandas to understand that this is not the correct way to do it. My goal is then to plot the score 4 for a given number (x-Axis) with boxplots. Thus, every box might not contain the same number of scores because the value number for a given ID is not fixed.