如何在熊猫数据框中将条目数添加为新行?

时间:2019-11-26 17:34:19

标签: python pandas row series

我正在使用Python,并且有如下系列:

            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

在计数,均值和标准差类别之后,我需要一个名为条目数的新行,其中包括每个组的条目数(用于视图计数的条目数,用于注释计数的条目数等)。实际上,我可以使用.info()选项获得条目数,它给了我以下结果:

<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

但是我不知道如何在我的系列中将这些条目数添加为新行。有没有人可以帮助我解决这个问题?

我的系列应​​该像这样:

            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    

2 个答案:

答案 0 :(得分:1)

我们可以使用DataFrame.count

  

对于每列/行,非NA /空条目的数量。

如果要按列计数并添加新行:

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

示例数据帧的输出:

         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  

如果要按行计数并创建新列:

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

输出:

       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 

答案 1 :(得分:1)

您可以使用以下行:

df['new_col'] = df.notnull().sum(axis=1)

它为您提供每行非空值的数量(或者您希望每列非空值?)。如果您有4行:

Out[87]: 
0    6
1    5
2    6
3    6
dtype: int64