0

i have a dataframe with 81 columns. Only four of them are interesting for more at the moment.

They are called 'Transmission' (object), 'Composite City MPG' (int64), 'Composite Highway MPG' (int64) and 'Composite Combined MPG' (int64).

I want the average of the MPG to each type of Transmission.

Thats what i tried.

data.groupby(['Transmission'], sort=True).mean()

How can i fix the code so that only the average of the relevant columns ('Composite City MPG' (int64), 'Composite Highway MPG' (int64) and 'Composite Combined MPG' (int64)) are shown?

Is groupby actually a good way to look for the average MPG for each type of transmission?

Bindl
  • 81
  • 2
  • 6
  • please post a small reproducible data set (in text form) and your desired data set. Please read [how to make good reproducible pandas examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and edit your post correspondingly. – MaxU - stand with Ukraine Dec 25 '17 at 12:14

2 Answers2

0
data.groupby('Transmission' ,\
'Composite City MPG', \
'Composite Highway MPG',\
'Composite Combined MPG')['MPG'].mean()
Binyamin Even
  • 3,318
  • 1
  • 18
  • 45
  • Thanks for the answer. I guess i explained it not that great. The MPG are in the 3 columns 'Composite City MPG' (int64), 'Composite Highway MPG' (int64) and 'Composite Combined MPG' (int64). I changed it a bit to this: data.groupby('Transmission', sort=True)[['Composite City MPG','Composite Highway MPG','Composite Combined MPG']].mean() and it works perfectly, thanks! – Bindl Dec 25 '17 at 11:47
0

with your help i change it a bit to get the result i needed. The MPG are in the 3 columns 'Composite City MPG' (int64), 'Composite Highway MPG' (int64) and 'Composite Combined MPG' (int64).

I used:

data.groupby('Transmission', sort=True)[['Composite City MPG','Composite Highway MPG','Composite Combined MPG']].mean()

Is there a way to use .isnull() and .notnull() that only the rows with averages that are null or not null are shown? I tried it with the apply method, but the outcome is not what i want:

.apply(lambda x: x.notnull())
Bindl
  • 81
  • 2
  • 6