Python 如何在数据框中添加条目数作为新行?

Python 如何在数据框中添加条目数作为新行?,python,pandas,row,series,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

我正在使用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
在count、mean和std类别之后,我需要一个名为number of entries的新行,其中包括每个组的条目数(查看计数的条目数、注释计数的条目数等)。实际上,我可以通过使用
.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
我们可以使用:

对于每一列/行,非NA/null条目的数量

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

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 
示例数据帧的输出:

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 

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

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 
输出:

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 
我们可以使用:

对于每一列/行,非NA/null条目的数量

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

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 
示例数据帧的输出:

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 

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

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 
输出:

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 
您可以使用以下行:

df['new_col'] = df.notnull().sum(axis=1)
它为您提供了每行(或您是否希望每列?)的非空值数。如果有4行:

Out[87]: 
0    6
1    5
2    6
3    6
dtype: int64
您可以使用以下行:

df['new_col'] = df.notnull().sum(axis=1)
它为您提供了每行(或您是否希望每列?)的非空值数。如果有4行:

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

我想你想要一个新的列,而不是新行,对吗?或者你能举个例子,在这种情况下,行是什么样子的?我编辑了我的问题。你可以看到我现在想做什么。如果不可能,我们可以添加一个新列,也许?我愿意选择:)但这是一样的,因为行
计数
不是吗?从
信息
返回的
条目
的值与您调用的
计数
相同。您已经有了row
count
它工作了!谢谢大家!我想你想要一个新的列,而不是新行,对吗?或者你能举个例子,在这种情况下,行是什么样子的?我编辑了我的问题。你可以看到我现在想做什么。如果不可能,我们可以添加一个新列,也许?我愿意选择:)但这是一样的,因为行
计数
不是吗?从
信息
返回的
条目
的值与您调用的
计数
相同。您已经有了row
count
它工作了!谢谢大家!这是一个很好的答案,但我有一个问题。我不想在这个系列中增加条目的数量。让我澄清一下:我有一个叫做dodo_数据的系列。我使用了dodo_data.descripe()代码,得到了您所看到的序列。我正在尝试将dodo_数据中的条目数添加到您现在看到的系列中。请显示您的原始数据帧。。。如果它有相同的列,你只需要在DataFrame中使用count,这是一个很好的答案,但我有一个问题。我不想在这个系列中增加条目的数量。让我澄清一下:我有一个叫做dodo_数据的系列。我使用了dodo_data.descripe()代码,得到了您所看到的序列。我正在尝试将dodo_数据中的条目数添加到您现在看到的系列中。请显示您的原始数据帧。。。如果它具有相同的列,则只需对该数据帧使用count