0
df = {'category': ['retail', 'retail', 'airtime', 'electronic', 'retail', 'electronic', 
'retail', 'retail','airtime'],
'default': [0, 1, 1, 1,0,1,1,1,0],
'year': [2011, 2013, 2014, 2015,2016,2017,2018,2010,2009],
'value': [6000, 6500, 3000, 1200,3200,4530,2100,1000,3400]}
df = pd.DataFrame(df2)
df
df[['category'=='retail', 'default'==1, 'value']].value.counts()
df[['category'=='retail', 'default'==1, 'value']].mean()

My expected output should look like the image below and the mean of the extracted value column

enter image description here

  • 2
    What do you mean "mean of the extracted value column"? The output frame is just filtered no means are taken. – Henry Ecker Aug 24 '21 at 00:05
  • what I meant is this, after the desired output, I want to know the mean(average) of the value column – umagba alex Aug 24 '21 at 00:22
  • 1
    The dataframe can be filtered by acessing the column values: `new_df = df[df['category'].eq('retail') & df['default'].eq(1)].copy()` your conditions must produce valid boolean series and correct logic needs to be used to combine them. – Henry Ecker Aug 24 '21 at 00:39
  • 1
    Once you've filtered you can take the `mean` -> `new_df['value'].mean()` or get to the mean directly from the unfiltered dataframe with `loc`: -> `df.loc[df['category'].eq('retail') & df['default'].eq(1), 'value'].mean()` – Henry Ecker Aug 24 '21 at 00:40
  • It may be a good idea to refresh yourself on the [Indexing and selecting data](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html) section of the `pandas` documentation. Specifically the section on [Boolean indexing](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#boolean-indexing) – Henry Ecker Aug 24 '21 at 00:42

1 Answers1

0
pd.pivot_table(df,index=['category','default'],values='value',aggfunc='mean')

pivot table may help.

balalala
  • 92
  • 1
  • 10