python - pandas 'as_index' function doesn't work as expected -
this minimum reproducible example of original dataframe called 'calls':
phone_number call_outcome agent call_number 0 83473306392 not interested orange 0 1 762850680150 call later orange 1 2 476309275079 not interested orange 2 3 899921761538 call later red 3 4 906739234066 call later orange 4
writing pandas command...
most_calls = calls.groupby('agent') \ .count().sort('call_number', ascending=false)
returns this...
phone_number call_outcome call_number agent orange 2234 2234 2234 red 1478 1478 1478 black 750 750 750 green 339 339 339 blue 199 199 199
which correct, fact want 'agent' variable , not indexed.
i've used as_index=false
function on numerous occasions , familiar specifying axis=1
. in instance doesn't matter or how incorporate these parameters, every permutation returns error.
these examples i've tried , corresponding errors:
most_calls = calls.groupby('agent', as_index=false) \ .count().sort('call_number', ascending=false) valueerror: invalid literal long() base 10: 'black'
and
most_calls = calls.groupby('agent', as_index=false, axis=1) \ .count().sort('call_number', ascending=false) valueerror: as_index=false valid axis=0
i believe that, irrespective of groupby
operation you've done, need call reset_index
index column should regular column.
starting mockup of data:
import pandas pd calls = pd.dataframe({ 'agent': ['orange', 'red'], 'phone_number': [2234, 1478], 'call_outcome': [2234, 1478], }) >> calls agent call_outcome phone_number 0 orange 2234 2234 1 red 1478 1478
here operation did reset_index()
appended:
>> calls.groupby('agent').count().sort('phone_number', ascending=false).reset_index() agent call_outcome phone_number 0 orange 1 1 1 red 1 1
Comments
Post a Comment