Home > Software design >  I want to count the values in one column based on a condition of another
I want to count the values in one column based on a condition of another

Time:01-25

OP_CARRIER WHY_DELAY
WN WEATHER
DL 0
AA CARRIER

Each row is a flight into Miami International Airport. WHY DELAY is a column that states why the flight was delayed, if the value is 0, that means that the flight was on time. I am trying to count how many flights were delayed by airline.

df.loc[(df['WHY_DELAY']!= 0)].groupby('OP_CARRIER').value_counts()

CodePudding user response:

If all you want is the number that satisfy a criterion:

df[df['WHY_DELAY'] == 0].shape[0] 

Produces a count of the number of rows matching the specified criterion, in this case 1

CodePudding user response:

Changing value_counts() to count() will give you the count of none zero grouped by OP_CARRIER.

df = pd.DataFrame({'OP_CARRIER': ['WN',  'DL', 'AA', 'DL', 'AA'],
                   'WHY_DELAY': ['WEATHER', 0, 'CARRIER', 'ALIENS', 'ASTROID']})

df.loc[(df['WHY_DELAY']!= 0)].groupby('OP_CARRIER').count()
OP_CARRIER WHY_DELAY
'AA' 2
'DL' 1
'WN' 1

CodePudding user response:

df[df.WHY_DELAY!='0'].groupby('OP_CARRIER').size()
Out[19]: 
OP_CARRIER
AA    1
WN    1

if you want with column_names then

df[df.WHY_DELAY!='0'].groupby('OP_CARRIER').count().reset_index()
Out[21]: 
  OP_CARRIER  WHY_DELAY
0         AA          1
1         WN          1
  • Related