0

I am writing a function which tries to find a 'string' from a column of a dataframe and then group the dataframe based on two other columns and count the values. However, I am getting a null key value after count which causes the issue while writing the dataframe back to MongoDB.

df is the dataframe with columns such as ConversationDate, panelistId, and contentType

def get_total_daily_interactions(df):
    df['interactions_count_asr']=1
    df_total_daily_interactions = df[df.contentType == 'ASR'].groupby(['conversationDate','panelistId']).count([['interactions_count_asr']]

return df_total_daily_interactions

This is what I am getting as an output

                                                      interactions_count_asr
conversationDate panelistId                                                  
4/26/2018        08d1b9b1-e99d-4c42-9ca5-9c9bc0b748e7                      11
4/27/2018        08d1b9b1-e99d-4c42-9ca5-9c9bc0b748e7                       3
4/29/2018        08d1b9b1-e99d-4c42-9ca5-9c9bc0b748e7                     103

Why am I getting the 'Null' key value for intercations_count_asr? How can this code be improved? I want to have a dataframe which provides interactions_count for panelistId based on conversationDate.

  • What null key is there? It looks like the two columns are your multiindex which offsets the first row when you print the frame. – user3483203 Jul 24 '19 at 13:38
  • Thanks for your reply. I used the answer from the following thread which worked for me. However, I don't understand why the first raw of the count will get NULL. It happens every time when I use 'groupby' followed by 'count.' https://stackoverflow.com/questions/10373660/converting-a-pandas-groupby-output-from-series-to-dataframe. – Kunal Raval Jul 24 '19 at 15:35

0 Answers0