Filter for column value and set its other column to an array

Question:

I have a df such as

Letter | Stats
  B       0
  B       1
  C       22
  B       0
  C       0
  B       3

How can I filter for a value in the Letter column and also then convert the stats column for that value into an array?

Basically want to filter for B and convert the Stats column to an array, Thanks!

Asked By: Chris90

||

Answers:

here is one way to do it

# function received, dataframe and letter as parameter
# return stats values as list for the passed Letter
def grp(df, letter):
    return df.loc[df['Letter'].eq(letter)]['Stats'].values.tolist()

# pass the dataframe, and the letter 
result=grp(df,'B')
print(result)

[0, 1, 0, 3]

data used

data ={'Letter': {0: 'B', 1: 'B', 2: 'C', 3: 'B', 4: 'C', 5: 'B'},
 'Stats': {0: 0, 1: 1, 2: 22, 3: 0, 4: 0, 5: 3}}
df=pd.DataFrame(data)
Answered By: Naveed

Although I believe that solution proposed by @Naveed is enough for this problem one little extension could be suggested.

If you would like to get result as an pandas series and obtain some statistic for the series:

data ={'Letter': {0: 'B', 1: 'B', 2: 'C', 3: 'B', 4: 'C', 5: 'B'},
 'Stats': {0: 0, 1: 1, 2: 22, 3: 0, 4: 0, 5: 3}}

df = pd.DataFrame(data)

letter = 'B'
ser = pd.Series(name=letter, data=df.loc[df['Letter'].eq(letter)]['Stats'].values)

print(f"Max value: {ser.max()} | Min value: {ser.min()} | Median value: {ser.median()}") etc.

Output:
Max value: 3 | Min value: 0 | Median value: 0.5

Answered By: 1001001
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.