Search code examples
pythonpandasrowseries

How can I add number of entries as a new row in pandas data frame?


I am working with Python and have a series which is as follows:

            view_count    comment_count like_count   dislike_count  ratio_of_comments_per_view  ratio_of_likes_per_view
count      2.200000e+01     21.000000    22.000000      22.000000            21.000000          22.000000
mean       1.481812e+06     4547.523810  49981.863636   667.136364           0.002539            0.037818
std        2.263283e+06     8716.083952  79607.504617   1249.618086          0.001072            0.010861

After count, mean and std categories, I need a new row called number of entries which include number of entries for each group (number of entries for view count, number of entries for comment count etc.). Actually I could get number of entries by using .info() option and it gave me the following results:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 22 entries, 2 to 67
Data columns (total 8 columns):
title                         22 non-null object
view_count                    22 non-null int64
comment_count                 21 non-null float64
like_count                    22 non-null int64
dislike_count                 22 non-null int64
ratio_of_comments_per_view    21 non-null float64
ratio_of_likes_per_view       22 non-null float64
other_tag                     22 non-null object
dtypes: float64(3), int64(3), object(2)
memory usage: 1.5+ KB

But I do not know how to add these number of entries as a new row in my series. Is there anyone who can help me with this problem?

My series should look like this:

            view_count    comment_count like_count   dislike_count  ratio_of_comments_per_view  ratio_of_likes_per_view
count      2.200000e+01     21.000000    22.000000      22.000000            21.000000          22.000000
mean       1.481812e+06     4547.523810  49981.863636   667.136364           0.002539            0.037818
std        2.263283e+06     8716.083952  79607.504617   1249.618086          0.001072            0.010861
#entries         22                21         22         22                   21        22    

Solution

  • We can use DataFrame.count:

    For each column/row the number of non-NA/null entries.

    If you want count by columns and add a new row:

    df=df.append(df.count().to_frame('entries').T)
    print(df)
    

    output for the sample dataframe:

             view_count  comment_count    like_count  dislike_count  \
    count          22.0      21.000000     22.000000      22.000000   
    mean      1481812.0    4547.523810  49981.863636     667.136364   
    std       2263283.0    8716.083952  79607.504617    1249.618086   
    entries         3.0       3.000000      3.000000       3.000000   
    
             ratio_of_comments_per_view  ratio_of_likes_per_view  
    count                     21.000000                22.000000  
    mean                       0.002539                 0.037818  
    std                        0.001072                 0.010861  
    entries                    3.000000                 3.000000  
    

    If you want count by rows and create a new column:

    df['entries']=df.count(axis=1)
    print(df)
    

    Output:

           view_count  comment_count    like_count  dislike_count  \
    count        22.0      21.000000     22.000000      22.000000   
    mean    1481812.0    4547.523810  49981.863636     667.136364   
    std     2263283.0    8716.083952  79607.504617    1249.618086   
    
           ratio_of_comments_per_view  ratio_of_likes_per_view  entries  
    count                   21.000000                22.000000        6  
    mean                     0.002539                 0.037818        6  
    std                      0.001072                 0.010861        6